/*
 *  myangio.cpp
 *  fix
 *
 *  Created by wumin on 8/2/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *should i reverse the list???
 Yes, do it
 DON'T FORGET TO SET SPROUTTIP BACK TO 0
 */
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

#include "../ImprovedMath.h"
#include "../random.h"
#include "../ParseText.h"
#include "myangio.h"
#include "../Plotting.h"

#include "../Matrix.h"

extern double AngioDeltaT;
double AngioDeltaY;
double AngioDeltaX;
double FlowDeltaT;
double MaxTAF;
double MaxWss;
double FlowMax;
bool VesselBroke;
int touchpoints;

extern bool FlowBegin;
extern bool AdaptionBegin;
extern char* ParameterFile;
Matrix Angio_Iteration_Change_Matrix;

VesselCell::VesselCell()
{ 
 coverlimit = 10;
 cConnectRange = (coverlimit+1)*2;
 cIndex = 0;
 cSetPressure = 0;
 cCover = 0;
 cConnection = 0;
 cFate = 'S';
 cReceptor = 0;
 cRadius = 1;
 cPermeability = 0.0;
 cPericytes = 0;
 //cConnectIndex = zeros(1,6);
 
 
 pNextVesselCell = NULL;
 pPreviousVesselCell = NULL;
} 



VesselCell::~VesselCell()
{
cout<<"One Cell died";
}





CellVesselStructure::CellVesselStructure()
{
	pFirstVesselCell = NULL;
	pLastVesselCell = NULL;
	SproutNumber = 0;
	
	CellListLength = 0;
	//cout <<"function VesselStructure running"<<endl;	
}

CellVesselStructure::~CellVesselStructure()
{
//cout <<"function ~VesselStructure running"<<endl;	
 VesselCell* pCurrentVesselCell = pFirstVesselCell;
 if( pCurrentVesselCell)
 {
	while(pCurrentVesselCell)
	{
		pCurrentVesselCell = pCurrentVesselCell ->pNextVesselCell;
		delete  pCurrentVesselCell->pPreviousVesselCell;
	}
	delete  pCurrentVesselCell;
 }
}





void CellVesselStructure::InitializeCellTime(bool Resume)
{
 
//cout <<"function Initializetime running"<<endl;
 extern double t;
 FlowBegin = (bool) RetrieveDoubleParameter( ParameterFile , "FlowBegin");
 AdaptionBegin = (bool) RetrieveDoubleParameter( ParameterFile , "AdaptionBegin");
 if(t>0)
 {
	 FlowBegin=1;
	 AdaptionBegin=1;
 }
 if( Resume == 1)
 {
 /*
 extern char SaveDirectory[1025];
 char temp_string[1024];
 
	 sprintf( temp_string , "%s/angio_resume.dat" , SaveDirectory );
 ifstream angio_resume( temp_string, ios::in );
 angio_resume >> AngioDeltaT;
			 
 cout<<AngioDeltaT<<"   "<<endl;
            
 angio_resume.close();
 */
 }
 else
 {
	AngioDeltaT = 0.05; //THIS IS WHERE I'M NOT SURE OF
	cout<<"AngioDeltaT"<<AngioDeltaT<<endl;
	

	
 }
//cout <<"function Initializetime finished"<<endl; 
}	




void CellVesselStructure::InitializeCellList(bool VesselResume)
{	
	
	
	extern int Xnodes;
	extern int Ynodes;
	//Indicator = zeros(Xnodes, Ynodes);
	extern bool AngiogenesisIsStarted;
	pFirstVesselCell = new VesselCell;
	pFirstVesselCell->pPreviousVesselCell = NULL;
	
	if (VesselResume == 0 && !AngiogenesisIsStarted)
	{
		extern int Xnodes;
		extern int Ynodes;
		int InitialListLength = 40;
		
		//int InitialSprout = 5;
		
		VesselCell* pCurrentVesselCell = pFirstVesselCell;
		
		/****empty point*******/	
		
		pCurrentVesselCell->cIndex = 0;
		pCurrentVesselCell->i[0] =-1;
		pCurrentVesselCell->j[0] = -1;
		
		pCurrentVesselCell->cConnection = 0;
		pCurrentVesselCell->cSetPressure = 0;
		pCurrentVesselCell->cConnectIndex[0] =0;
		pCurrentVesselCell->cPericytes =0;
		
		VesselCell* pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		/*******empty point*********/
		
	SproutNumber = 0; 
	CellListLength = 0;


    int unitsize = 33;
	
	int div = (Xnodes - 1)/2;
	int len = 3;//coverlimit	
	int i=0;
	int j=0;
	int index=1;
			
    int unitindex = 0;//0-3
	int i_art = 0;//1-99
	int j_art = 0;//0,100,200
	int i_ven = 50;//101-199
	int j_ven = 0;//50,150

/*************************1*******************************/
	
	i=i_art;
	j=j_art;
    unitsize = 5;
    index = 1;
	len = 0;
	 CellListLength++;
	  pCurrentVesselCell->cCover = 9;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8;
		pCurrentVesselCell->j[8] = j;


		
	  pCurrentVesselCell->cConnection = 1;
	  pCurrentVesselCell->cSetPressure =1;
	  pCurrentVesselCell->cConnectIndex[0] = index+1;
	  
	  pCurrentVesselCell->cPericytes =1;
	 
	  
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
	  pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
	  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
   
  
		for(index=2; index<unitsize; index++)
		{
		    len++;
			CellListLength++;
			  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10-1;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10-1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10-1;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10-1;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10-1;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10-1;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10-1;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10-1;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10-1;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10-1;
		pCurrentVesselCell->j[9] = j;

		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = index+1;
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	   
	   
		}
		
		len++;
		CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10-1;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10-1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10-1;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10-1;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10-1;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10-1;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10-1;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10-1;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10-1;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10-1;
		pCurrentVesselCell->j[9] = j;


		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = 81;
				
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		
	
	
	/*************************2*******************************/
	
	i=i_art;
	j=j_art+50;
    index = 21;
	unitsize = 25;
    len = 0;
	 CellListLength++;
	  pCurrentVesselCell->cCover = 9;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8;
		pCurrentVesselCell->j[8] = j;


		
	  pCurrentVesselCell->cConnection = 1;
	  pCurrentVesselCell->cSetPressure =1;
	  pCurrentVesselCell->cConnectIndex[0] = index+1;
	  
	  pCurrentVesselCell->cPericytes =1;
	 
	  
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
	  pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
	  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
   
  
		for(index=22; index<unitsize; index++)
		{
		    len++;
			CellListLength++;
			  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
	pCurrentVesselCell->i[0] = i+len*10-1;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10-1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10-1;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10-1;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10-1;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10-1;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10-1;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10-1;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10-1;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10-1;
		pCurrentVesselCell->j[9] = j;

		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = index+1;
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	   
	   
		}
		
		len++;
		CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
			pCurrentVesselCell->i[0] = i+len*10-1;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10-1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10-1;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10-1;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10-1;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10-1;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10-1;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10-1;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10-1;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10-1;
		pCurrentVesselCell->j[9] = j;

				
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = 86;		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		
	
	/*************************3*******************************/
	len = 0;
	i=i_art;
	j=j_art+150;
    index = 41;
	unitsize = 45;
    
	 CellListLength++;
	  pCurrentVesselCell->cCover = 9;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8;
		pCurrentVesselCell->j[8] = j;


		
	  pCurrentVesselCell->cConnection = 1;
	  pCurrentVesselCell->cSetPressure =1;
	  pCurrentVesselCell->cConnectIndex[0] = index+1;
	  
	  pCurrentVesselCell->cPericytes =1;
	 
	  
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
	  pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
	  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
   
  
		for(index=42; index<unitsize; index++)
		{
			len++;
			CellListLength++;
			  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
			pCurrentVesselCell->i[0] = i+len*10-1;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10-1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10-1;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10-1;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10-1;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10-1;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10-1;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10-1;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10-1;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10-1;
		pCurrentVesselCell->j[9] = j;


		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = index+1;
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	   
	   
		}
		
		len++;
		CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
			pCurrentVesselCell->i[0] = i+len*10-1;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10-1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10-1;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10-1;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10-1;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10-1;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10-1;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10-1;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10-1;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10-1;
		pCurrentVesselCell->j[9] = j;


				
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = 96;		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		
	
		/*************************4*******************************/
	len = 0;
	i=i_art;
	j=j_art+200;
    index = 61;
	unitsize = 65;
    
	 CellListLength++;
	  pCurrentVesselCell->cCover = 9;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8;
		pCurrentVesselCell->j[8] = j;


		
	  pCurrentVesselCell->cConnection = 1;
	  pCurrentVesselCell->cSetPressure =1;
	  pCurrentVesselCell->cConnectIndex[0] = index+1;
	  
	  pCurrentVesselCell->cPericytes =1;
	 
	  
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
	  pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
	  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
   
  
		for(index=62; index<unitsize; index++)
		{
			len++;
			CellListLength++;
			  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
			pCurrentVesselCell->i[0] = i+len*10-1;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10-1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10-1;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10-1;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10-1;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10-1;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10-1;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10-1;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10-1;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10-1;
		pCurrentVesselCell->j[9] = j;


		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = index+1;
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	   
	   
		}
		
		len++;
		CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
			pCurrentVesselCell->i[0] = i+len*10-1;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10-1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10-1;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10-1;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10-1;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10-1;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10-1;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10-1;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10-1;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10-1;
		pCurrentVesselCell->j[9] = j;
				
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = 102;		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		
	
	
	/************************middle -*******************/
		
			
			
			/*************************1*******************************/
	
	i=i_art+50;
	j=j_art;
    index = 6;
	unitsize = 15;
    len = 0;
	
	 CellListLength++;
	  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9;
		pCurrentVesselCell->j[9] = j;



		
	  pCurrentVesselCell->cConnection = 2;
	  pCurrentVesselCell->cSetPressure =1;
	  pCurrentVesselCell->cConnectIndex[0] = index+1;
	  pCurrentVesselCell->cConnectIndex[1] = 81;

	  
	  pCurrentVesselCell->cPericytes =1;
	 
	  
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
	  pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
	  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
   
  
		for(index=7; index<unitsize; index++)
		{
			len++;
			CellListLength++;
			  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;

		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = index+1;
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	   
	   
		}
		
		len++;
		CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;


				
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = 103;		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		
			/*************************2*******************************/
	
	i=i_art+50;
	j=j_art+50;
    index = 26;
	unitsize = 35;
    len = 0;
	 CellListLength++;
	  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;




		
	  pCurrentVesselCell->cConnection = 2;
	  pCurrentVesselCell->cSetPressure =1;
	  pCurrentVesselCell->cConnectIndex[0] = index+1;
	  pCurrentVesselCell->cConnectIndex[1] = 86;

	  
	  pCurrentVesselCell->cPericytes =1;
	 
	  
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
	  pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
	  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
   
  
		for(index=27; index<unitsize; index++)
		{
			len++;
			CellListLength++;
			  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;



		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = index+1;
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	   
	   
		}
		
		len++;
		CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;


				
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = 108;		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		
			
		
				
			
			/*************************3*******************************/
	
	i=i_art+50;
	j=j_art+150;
    index = 46;
	unitsize = 55;
    len = 0;
	 CellListLength++;
	  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9;
		pCurrentVesselCell->j[9] = j;



		
	  pCurrentVesselCell->cConnection = 2;
	  pCurrentVesselCell->cSetPressure =1;
	  pCurrentVesselCell->cConnectIndex[0] = index+1;
	  pCurrentVesselCell->cConnectIndex[1] = 96;

	  
	  pCurrentVesselCell->cPericytes =1;
	 
	  
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
	  pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
	  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
   
  
		for(index=47; index<unitsize; index++)
		{
			len++;
			CellListLength++;
			  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;


		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = index+1;
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	   
	   
		}
		
		len++;
		CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;


				
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = 118;		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
							
		
								
																		
			
			/*************************4*******************************/
	
	i=i_art+50;
	j=j_art+200;
    index = 66;
	unitsize = 75;
    len = 0;
	 CellListLength++;
	  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9;
		pCurrentVesselCell->j[9] = j;



		
	  pCurrentVesselCell->cConnection = 2;
	  pCurrentVesselCell->cSetPressure =1;
	  pCurrentVesselCell->cConnectIndex[0] = index+1;
	  pCurrentVesselCell->cConnectIndex[1] = 102;

	  
	  pCurrentVesselCell->cPericytes =1;
	 
	  
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
	  pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
	  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
   
  
		for(index=67; index<unitsize; index++)
		{
			len++;
			CellListLength++;
			  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;


		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = index+1;
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	   
	   
		}
		len++;
		CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;


				
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = 124;		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
								
		
												
/*************************right - *******************************/

/*************************1***********************************/
	
	i=i_art+151;
	j=j_art;
    index = 16;
	unitsize = 20;
    len = 0;
	 CellListLength++;
	  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9;
		pCurrentVesselCell->j[9] = j;



		
	  pCurrentVesselCell->cConnection = 2;
	  pCurrentVesselCell->cSetPressure =1;
	  pCurrentVesselCell->cConnectIndex[0] = index+1;
	  pCurrentVesselCell->cConnectIndex[1] = 103;

	  
	  pCurrentVesselCell->cPericytes =1;
	 
	  
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
	  pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
	  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
   
  
		for(index=17; index<unitsize; index++)
		{
			len++;
			CellListLength++;
			  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;


		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = index+1;
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	   
	   
		}
		len++;
		CellListLength++;
		pCurrentVesselCell->cCover = 9;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		
	

				
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = 125;		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
												
		
						
	/***********************************2******************/									
	i=i_art+151;
	j=j_art+50;
    index = 36;
	unitsize = 40;
    len = 0;
	 CellListLength++;
	  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9;
		pCurrentVesselCell->j[9] = j;



		
	  pCurrentVesselCell->cConnection = 2;
	  pCurrentVesselCell->cSetPressure =1;
	  pCurrentVesselCell->cConnectIndex[0] = index+1;
	  pCurrentVesselCell->cConnectIndex[1] = 108;

	  
	  pCurrentVesselCell->cPericytes =1;
	 
	  
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
	  pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
	  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
   
  
		for(index=37; index<unitsize; index++)
		{
			len++;
			CellListLength++;
			  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;


		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = index+1;
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	   
	   
		}
		len++;
		CellListLength++;
		pCurrentVesselCell->cCover = 9;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		
	

				
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = 130;		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
												
														
		/***********************************3******************/									
	i=i_art+151;
	j=j_art+150;
    index = 56;
	unitsize = 60;
    len = 0;
	 CellListLength++;
	  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9;
		pCurrentVesselCell->j[9] = j;



		
	  pCurrentVesselCell->cConnection = 2;
	  pCurrentVesselCell->cSetPressure =1;
	  pCurrentVesselCell->cConnectIndex[0] = index+1;
	  pCurrentVesselCell->cConnectIndex[1] = 118;

	  
	  pCurrentVesselCell->cPericytes =1;
	 
	  
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
	  pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
	  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
   
  
		for(index=57; index<unitsize; index++)
		{
			len++;
			CellListLength++;
			  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;


		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = index+1;
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	   
	   
		}
		len++;
		CellListLength++;
		pCurrentVesselCell->cCover = 9;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		
	

				
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = 140;		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
	
																				
		/***********************************4******************/									
	i=i_art+151;
	j=j_art+200;
    index = 76;
	unitsize = 80;
    len = 0;
	  CellListLength++;
	  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9;
		pCurrentVesselCell->j[9] = j;



		
	  pCurrentVesselCell->cConnection = 2;
	  pCurrentVesselCell->cSetPressure =1;
	  pCurrentVesselCell->cConnectIndex[0] = index+1;
	  pCurrentVesselCell->cConnectIndex[1] = 124;

	  
	  pCurrentVesselCell->cPericytes =1;
	 
	  
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
	  pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
	  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
   
  
		for(index=77; index<unitsize; index++)
		{
			len++;
			CellListLength++;
			  pCurrentVesselCell->cCover = 10;
	  pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		pCurrentVesselCell->i[9] = i+9+len*10;
		pCurrentVesselCell->j[9] = j;


		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = index+1;
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	   
	   
		}
		len++;
		CellListLength++;
		pCurrentVesselCell->cCover = 9;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i+len*10;
		pCurrentVesselCell->j[0] = j;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i+1+len*10;
		pCurrentVesselCell->j[1] = j;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i+2+len*10;
		pCurrentVesselCell->j[2] = j;
		
		pCurrentVesselCell->i[3] = i+3+len*10;
		pCurrentVesselCell->j[3] = j;
		
		pCurrentVesselCell->i[4] = i+4+len*10;
		pCurrentVesselCell->j[4] = j;
		
		pCurrentVesselCell->i[5] = i+5+len*10;
		pCurrentVesselCell->j[5] = j;
		
		pCurrentVesselCell->i[6] = i+6+len*10;
		pCurrentVesselCell->j[6] = j;
		
		pCurrentVesselCell->i[7] = i+7+len*10;
		pCurrentVesselCell->j[7] = j;
		
		pCurrentVesselCell->i[8] = i+8+len*10;
		pCurrentVesselCell->j[8] = j;
		
		
	

				
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = index-1;
		pCurrentVesselCell->cConnectIndex[1] = 146;		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;																		
																
	/*************************|********************************/																					
																										
																																		
//*********************1*************************/
		
		i=49;
		j=0;
		index=81;
		len = 0;
		
		CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*10;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i;
		pCurrentVesselCell->j[1] = j+1+len*10;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i;
		pCurrentVesselCell->j[2] = j+2+len*10;
		
		pCurrentVesselCell->i[3] = i;
		pCurrentVesselCell->j[3] = j+3+len*10;
		
		pCurrentVesselCell->i[4] = i;
		pCurrentVesselCell->j[4] = j+4+len*10;
		
		pCurrentVesselCell->i[5] = i;
		pCurrentVesselCell->j[5] = j+5+len*10;
		
		pCurrentVesselCell->i[6] = i;
		pCurrentVesselCell->j[6] = j+6+len*10;
		
		pCurrentVesselCell->i[7] = i;
		pCurrentVesselCell->j[7] = j+7+len*10;
		
		pCurrentVesselCell->i[8] = i;
		pCurrentVesselCell->j[8] = j+8+len*10;
		
		pCurrentVesselCell->i[9] = i;
		pCurrentVesselCell->j[9] = j+9+len*10;


		
		
		    pCurrentVesselCell->cConnection = 3;
			pCurrentVesselCell->cSetPressure =0;
			pCurrentVesselCell->cConnectIndex[0] = 5;
			pCurrentVesselCell->cConnectIndex[1] = 6;
			pCurrentVesselCell->cConnectIndex[2] = index+1;	
		
		
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		
		
		
		for(index=82;index<101;index++)
		{
			
			len++;
			CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*10;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i;
		pCurrentVesselCell->j[1] = j+1+len*10;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i;
		pCurrentVesselCell->j[2] = j+2+len*10;
		
		pCurrentVesselCell->i[3] = i;
		pCurrentVesselCell->j[3] = j+3+len*10;
		
		pCurrentVesselCell->i[4] = i;
		pCurrentVesselCell->j[4] = j+4+len*10;
		
		pCurrentVesselCell->i[5] = i;
		pCurrentVesselCell->j[5] = j+5+len*10;
		
		pCurrentVesselCell->i[6] = i;
		pCurrentVesselCell->j[6] = j+6+len*10;
		
		pCurrentVesselCell->i[7] = i;
		pCurrentVesselCell->j[7] = j+7+len*10;
		
		pCurrentVesselCell->i[8] = i;
		pCurrentVesselCell->j[8] = j+8+len*10;
		
		pCurrentVesselCell->i[9] = i;
		pCurrentVesselCell->j[9] = j+9+len*10;			
			if(index==86)
			{
				pCurrentVesselCell->cConnection = 4;
				pCurrentVesselCell->cSetPressure =0;
				pCurrentVesselCell->cConnectIndex[0] = index-1;
				pCurrentVesselCell->cConnectIndex[1] = index+1;
				pCurrentVesselCell->cConnectIndex[2] = 25;
				pCurrentVesselCell->cConnectIndex[3] = 26;
			}
			else if(index==96)
			{
				pCurrentVesselCell->cConnection = 4;
				pCurrentVesselCell->cSetPressure =0;
				pCurrentVesselCell->cConnectIndex[0] = index-1;
				pCurrentVesselCell->cConnectIndex[1] = index+1;	
				pCurrentVesselCell->cConnectIndex[2] = 45;
				pCurrentVesselCell->cConnectIndex[3] = 46;
			}
			else 
			{
				pCurrentVesselCell->cConnection = 2;
				pCurrentVesselCell->cSetPressure =0;
				pCurrentVesselCell->cConnectIndex[0] = index-1;
				pCurrentVesselCell->cConnectIndex[1] = index+1;	
			}

			
			pCurrentVesselCell->cPericytes =0;
			
			
			pNewVesselCell = new VesselCell;
			pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
			pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
			pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
			
		}
		
		
		len++;

		CellListLength++;
		pCurrentVesselCell->cCover = 1;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*10;
		
		
		
		pCurrentVesselCell->cConnection = 3;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = 65;
		pCurrentVesselCell->cConnectIndex[1] = 66;	
		pCurrentVesselCell->cConnectIndex[2] = index-1;	
		pCurrentVesselCell->cPericytes =1;
		
     		pNewVesselCell = new VesselCell;
			pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
			pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
			pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;

		
		
	
	
	//*********************2*************************/
		
		i=151;
		j=0;
		index=103;
		len = 0;
		
		CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*10;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i;
		pCurrentVesselCell->j[1] = j+1+len*10;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i;
		pCurrentVesselCell->j[2] = j+2+len*10;
		
		pCurrentVesselCell->i[3] = i;
		pCurrentVesselCell->j[3] = j+3+len*10;
		
		pCurrentVesselCell->i[4] = i;
		pCurrentVesselCell->j[4] = j+4+len*10;
		
		pCurrentVesselCell->i[5] = i;
		pCurrentVesselCell->j[5] = j+5+len*10;
		
		pCurrentVesselCell->i[6] = i;
		pCurrentVesselCell->j[6] = j+6+len*10;
		
		pCurrentVesselCell->i[7] = i;
		pCurrentVesselCell->j[7] = j+7+len*10;
		
		pCurrentVesselCell->i[8] = i;
		pCurrentVesselCell->j[8] = j+8+len*10;
		
		pCurrentVesselCell->i[9] = i;
		pCurrentVesselCell->j[9] = j+9+len*10;


		
		
		    pCurrentVesselCell->cConnection = 3;
			pCurrentVesselCell->cSetPressure =0;
			pCurrentVesselCell->cConnectIndex[0] = 15;
			pCurrentVesselCell->cConnectIndex[1] = 16;
			pCurrentVesselCell->cConnectIndex[2] = index+1;	
		
		
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		
		
		
		for(index=104;index<123;index++)
		{
			
			len++;
			CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*10;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i;
		pCurrentVesselCell->j[1] = j+1+len*10;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i;
		pCurrentVesselCell->j[2] = j+2+len*10;
		
		pCurrentVesselCell->i[3] = i;
		pCurrentVesselCell->j[3] = j+3+len*10;
		
		pCurrentVesselCell->i[4] = i;
		pCurrentVesselCell->j[4] = j+4+len*10;
		
		pCurrentVesselCell->i[5] = i;
		pCurrentVesselCell->j[5] = j+5+len*10;
		
		pCurrentVesselCell->i[6] = i;
		pCurrentVesselCell->j[6] = j+6+len*10;
		
		pCurrentVesselCell->i[7] = i;
		pCurrentVesselCell->j[7] = j+7+len*10;
		
		pCurrentVesselCell->i[8] = i;
		pCurrentVesselCell->j[8] = j+8+len*10;
		
		pCurrentVesselCell->i[9] = i;
		pCurrentVesselCell->j[9] = j+9+len*10;			
			if(index==108)
			{
				pCurrentVesselCell->cConnection = 4;
				pCurrentVesselCell->cSetPressure =0;
				pCurrentVesselCell->cConnectIndex[0] = index-1;
				pCurrentVesselCell->cConnectIndex[1] = index+1;
				pCurrentVesselCell->cConnectIndex[2] = 35;
				pCurrentVesselCell->cConnectIndex[3] = 36;
			}
			else if(index==118)
			{
				pCurrentVesselCell->cConnection = 4;
				pCurrentVesselCell->cSetPressure =0;
				pCurrentVesselCell->cConnectIndex[0] = index-1;
				pCurrentVesselCell->cConnectIndex[1] = index+1;	
				pCurrentVesselCell->cConnectIndex[2] = 55;
				pCurrentVesselCell->cConnectIndex[3] = 56;
			}
			else 
			{
				pCurrentVesselCell->cConnection = 2;
				pCurrentVesselCell->cSetPressure =0;
				pCurrentVesselCell->cConnectIndex[0] = index-1;
				pCurrentVesselCell->cConnectIndex[1] = index+1;	
			}

			
			pCurrentVesselCell->cPericytes = 0;
			
			
			pNewVesselCell = new VesselCell;
			pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
			pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
			pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
			
		}
		
		
		len++;

		CellListLength++;
		pCurrentVesselCell->cCover = 1;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*10;
		
		
		
		pCurrentVesselCell->cConnection = 3;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = 75;
		pCurrentVesselCell->cConnectIndex[1] = 76;	
		pCurrentVesselCell->cConnectIndex[2] = index-1;	
		pCurrentVesselCell->cPericytes =1;
		
     		pNewVesselCell = new VesselCell;
			pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
			pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
			pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;

			//*********************3*************************/
		
		i=200;
		j=0;
		index=125;
		len = 0;
		
		CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*10;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i;
		pCurrentVesselCell->j[1] = j+1+len*10;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i;
		pCurrentVesselCell->j[2] = j+2+len*10;
		
		pCurrentVesselCell->i[3] = i;
		pCurrentVesselCell->j[3] = j+3+len*10;
		
		pCurrentVesselCell->i[4] = i;
		pCurrentVesselCell->j[4] = j+4+len*10;
		
		pCurrentVesselCell->i[5] = i;
		pCurrentVesselCell->j[5] = j+5+len*10;
		
		pCurrentVesselCell->i[6] = i;
		pCurrentVesselCell->j[6] = j+6+len*10;
		
		pCurrentVesselCell->i[7] = i;
		pCurrentVesselCell->j[7] = j+7+len*10;
		
		pCurrentVesselCell->i[8] = i;
		pCurrentVesselCell->j[8] = j+8+len*10;
		
		pCurrentVesselCell->i[9] = i;
		pCurrentVesselCell->j[9] = j+9+len*10;


		
		
		    pCurrentVesselCell->cConnection = 2;
			pCurrentVesselCell->cSetPressure =0;
			pCurrentVesselCell->cConnectIndex[0] = 20;
			pCurrentVesselCell->cConnectIndex[1] = index+1;	
		
		
		
		pCurrentVesselCell->cPericytes =1;
		
		
		pNewVesselCell = new VesselCell;
		pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
		pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
		pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
		
		
		
		for(index=126;index<145;index++)
		{
			
			len++;
			CellListLength++;
		pCurrentVesselCell->cCover = 10;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*10;
		// Indicator[pCurrentVesselCell->i[0]][pCurrentVesselCell->j[0]] = 1;
		pCurrentVesselCell->i[1] = i;
		pCurrentVesselCell->j[1] = j+1+len*10;
		// Indicator[pCurrentVesselCell->i[1]][pCurrentVesselCell->j[1]] = 1;
		pCurrentVesselCell->i[2] = i;
		pCurrentVesselCell->j[2] = j+2+len*10;
		
		pCurrentVesselCell->i[3] = i;
		pCurrentVesselCell->j[3] = j+3+len*10;
		
		pCurrentVesselCell->i[4] = i;
		pCurrentVesselCell->j[4] = j+4+len*10;
		
		pCurrentVesselCell->i[5] = i;
		pCurrentVesselCell->j[5] = j+5+len*10;
		
		pCurrentVesselCell->i[6] = i;
		pCurrentVesselCell->j[6] = j+6+len*10;
		
		pCurrentVesselCell->i[7] = i;
		pCurrentVesselCell->j[7] = j+7+len*10;
		
		pCurrentVesselCell->i[8] = i;
		pCurrentVesselCell->j[8] = j+8+len*10;
		
		pCurrentVesselCell->i[9] = i;
		pCurrentVesselCell->j[9] = j+9+len*10;			
			if(index==130)
			{
				pCurrentVesselCell->cConnection = 3;
				pCurrentVesselCell->cSetPressure =0;
				pCurrentVesselCell->cConnectIndex[0] = index-1;
				pCurrentVesselCell->cConnectIndex[1] = index+1;
				pCurrentVesselCell->cConnectIndex[2] = 44;
				
			}
			else if(index==140)
			{
				pCurrentVesselCell->cConnection = 3;
				pCurrentVesselCell->cSetPressure =0;
				pCurrentVesselCell->cConnectIndex[0] = index-1;
				pCurrentVesselCell->cConnectIndex[1] = index+1;	
				pCurrentVesselCell->cConnectIndex[2] = 60;
				
			}
			else 
			{
				pCurrentVesselCell->cConnection = 2;
				pCurrentVesselCell->cSetPressure =0;
				pCurrentVesselCell->cConnectIndex[0] = index-1;
				pCurrentVesselCell->cConnectIndex[1] = index+1;	
			}

			
			pCurrentVesselCell->cPericytes = 0;
			
			
			pNewVesselCell = new VesselCell;
			pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell;
			pCurrentVesselCell->pNextVesselCell = pNewVesselCell;
			pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
			
		}
		
		
		len++;

		CellListLength++;
		pCurrentVesselCell->cCover = 1;
		pCurrentVesselCell->cIndex = index;
		
		pCurrentVesselCell->i[0] = i;
		pCurrentVesselCell->j[0] = j+len*10;
		
		
		
		pCurrentVesselCell->cConnection = 2;
		pCurrentVesselCell->cSetPressure =0;
		pCurrentVesselCell->cConnectIndex[0] = 80;
		pCurrentVesselCell->cConnectIndex[1] = index-1;	
		pCurrentVesselCell->cPericytes =1;
		
		
	 pCurrentVesselCell->pNextVesselCell = NULL;


	 pLastVesselCell = pCurrentVesselCell;
	 
	
	  
	

		//pLastVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;	
	//pLastVesselPoint -> pNextVesselPoint = NULL;

    }
	
	else
	{
		//meixianghao
		
	}
	//cout <<"function InitializeVesselListInOrder finished"<<endl; 
	
	
}



/*


bool CellVesselStructure::SaveVesselList()
{
//cout <<"function SaveListInOrder running"<<endl; 

extern bool AngiogenesisIsStarted;
extern bool AngiogenesisDisabled;

if( AngiogenesisIsStarted == false ||
     AngiogenesisDisabled == true )
 { 
 return false; 
 }	
 
 extern char SaveDirectory[1025];
 char temp_string[1024];
 
 sprintf( temp_string, "%s/angio_resume.dat", SaveDirectory);
 ofstream angio_resume( temp_string, ios::out );
 angio_resume << AngioDeltaT <<"\t"
			  
              		  
             << endl;	
cout<<AngioDeltaT<<" save  "<<endl;				  
 angio_resume.close();	
 
 
 //save list now
  sprintf( temp_string , "%s/pVesselList_resume.dat" , SaveDirectory );
 ofstream pVesselList_resume( temp_string, ios::out );

 VesselCell* pCurrentVesselCell = pFirstVesselCell;
  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;

 while(pCurrentVesselCell)
 {
 pVesselList_resume << pCurrentVesselCell->i<<"\t"
				    << pCurrentVesselCell->j<<endl;
					
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }
 
 pVesselList_resume.close();
// cout <<"function SaveListInOrder finished"<<endl;
 sprintf( temp_string , "%s/pVesselDistance.dat" , SaveDirectory );
 ofstream pVesselDistance( temp_string, ios::out );
 
 for(int i = 1; i< CellListLength; i++)
 {
 for(int j = 1; j< CellListLength; j++)
 {
	 pVesselDistance<< *Distance(i,j)<<"\t";
 }
 pVesselDistance<< endl;

 }
 
  return true;
 }
 

void CellVesselStructure::GenerateMatrix()
{

int i=0;
int j=0;
int N=0;
double d_x=0;
double d_y=0;
/*
Distance = zeros(CellListLength,CellListLength);
for(i=0;i<CellListLength;i++)
{
	for(j=0;j<CellListLength;j++)
	{
	*Distance(i,j) = -1;
	}
}



pRadius = zeros(CellListLength,CellListLength);
pPermeability = zeros(CellListLength,CellListLength);
pConduct = zeros(CellListLength,CellListLength);
pFlow = zeros(CellListLength,CellListLength);
pPericytes = zeros(CellListLength,CellListLength);

 VesselCell* pCurrentVesselCell = pFirstVesselCell;

 while(pCurrentVesselCell)
 {
 i = pCurrentVesselCell-> cIndex;
 N = pCurrentVesselCell->cConnection;
 for(int k = 0; k < N; k++)
 {
 j = pCurrentVesselCell->cConnectIndex[k];
 
 *Distance(i,j)=1;

 }
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }
*/
 
/*

 BloodVesselIndicator = zeros( Xnodes , Ynodes );
 BloodVesselIndicatorX = zeros( Xnodes , Ynodes );
 BloodVesselIndicatorY = zeros( Xnodes , Ynodes );
 HaematocritLevel = zeros( Xnodes , Ynodes );
 SproutTips = zeros( Xnodes , Ynodes );
 SproutAge = zeros(Xnodes, Ynodes);
 VesselAge = zeros(Xnodes, Ynodes);
 BloodVesselRadiusXdir = zeros(Xnodes , Ynodes);
 BloodVesselRadiusYdir = zeros(Xnodes , Ynodes);
 BloodVesselRadius = zeros( Xnodes , Ynodes );
 BloodNodePressure = zeros( Xnodes, Ynodes );
 BloodVesselPressureX = zeros( Xnodes, Ynodes );
//Direction Matrix only matters to Sprouts
    //DirectionX = zeros( Xnodes, Ynodes ); // 0 means no vessel, 1, -1 means one way direction
	//DirectionY = zeros( Xnodes, Ynodes );
	
	FlowX = zeros( Xnodes, Ynodes );
	FlowY = zeros( Xnodes, Ynodes );
	WssX = zeros( Xnodes, Ynodes );
	WssY = zeros( Xnodes, Ynodes );
	
	VisX = ones( Xnodes, Ynodes );
	VisY = ones( Xnodes, Ynodes );
	ConductX = zeros( Xnodes, Ynodes );
	ConductY = zeros( Xnodes, Ynodes );
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	
	//Min's Notes 1: Here is with the position of the ARTERY, fix here when changing position


	
	
	

	
	

  
	cout <<"function TransformMatrix finished"<<endl; 
}
*/

void CellVesselStructure::ShuffleFate(Matrix& Signal)
{
	int i=0;
	int j=0;
	double Threshold=0.0000001;
	
	//cout<<"TAF"<<*TAF(3,3)<<endl;
	/**All Stable defaultset**/
	VesselCell* pCurrentVesselCell = pFirstVesselCell;
	pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
 while(pCurrentVesselCell)
 {
 
	//cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;
 
	pCurrentVesselCell->cFate = 'S';
	
  					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 


		//getchar();			
	
	/****S\T****/
  pCurrentVesselCell = pFirstVesselCell;
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
	
	 while(pCurrentVesselCell)
 {
 
 //SetTipFate(pCurrentVesselCell, Signal, 'T', 'S',Threshold);

   SetTipFate(pCurrentVesselCell, Signal, 'S', 'S',Threshold);
	//	cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;			
									
		//cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<endl;				
																														
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 
 
// getchar();
 /*****T\P****/
  pCurrentVesselCell = pFirstVesselCell;
  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
 VesselCell* pCurrentVesselCell2 = pFirstVesselCell;
 pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;	
 int Index = 0;
	
	 while(pCurrentVesselCell)
 {
   if( pCurrentVesselCell->cFate == 'T')
   {
   for(int k=0; k< pCurrentVesselCell-> cConnection; k++)
   {
     Index = pCurrentVesselCell-> cConnectIndex[k];
	 //cout<<pCurrentVesselCell->cIndex<<":"<<Index<<endl;
	 pCurrentVesselCell2 = FindCell(Index);
	 if ( pCurrentVesselCell2->cFate == 'T' )
	 {
		CompeteFate(pCurrentVesselCell, pCurrentVesselCell2, Signal, 'T', 'P', 'P')	;	
	 }
   }
   
   }
						
						
	//	cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;
		//cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<endl;																													
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 
// getchar();	
 /************P\T***********/
 
   pCurrentVesselCell = pFirstVesselCell;
  pCurrentVesselCell2 = pFirstVesselCell;
  pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
  pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;	
    bool All_Not_T = 1; 

	 while(pCurrentVesselCell)
 {
  
   if( pCurrentVesselCell->cFate == 'P')
   {
   	
   
    All_Not_T = 1; 
   for(int k=0; k< pCurrentVesselCell-> cConnection; k++)
   {
     Index = pCurrentVesselCell-> cConnectIndex[k];
	 pCurrentVesselCell2 = FindCell(Index);
	
	
	 //Look at this
	 if (pCurrentVesselCell2->cFate == 'T' )
	 {
	
		All_Not_T = 0;	
	 }
   }
   
   if(All_Not_T==1)
   {
   pCurrentVesselCell->cFate = 'T';
   
   }
   
   }
						
		//cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<endl;
	
					
																															
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 

cout<<"after shuffle"<<endl;
 
 
 
}

void CellVesselStructure::SetSprout(Matrix& Signal)
{
int i1=0;
int j1=0;
int i_max=0;
int j_max=0;
double  Max_a = 0;
extern int Xnodes, Ynodes;
extern Matrix SproutTips;
SproutTips = zeros(Xnodes,Ynodes);

VesselCell*  pCurrentVesselCell = pFirstVesselCell;
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;	
	
	 while(pCurrentVesselCell)
 {
 Max_a = 0;
 
 if(pCurrentVesselCell->cFate == 'T')
 {
for(int ii=0; ii < pCurrentVesselCell->cCover; ii++)
{
i1 = pCurrentVesselCell->i[ii];
j1 = pCurrentVesselCell->j[ii];
if(*Signal(i1,j1)>Max_a)
{
Max_a = *Signal(i1,j1);
i_max=i1;
j_max=j1;
//cout<<"Sig  "<<*Signal(i1,j1)<<Max_a<<" max i "<<i_max<<" max j  "<<j_max<<endl;
}

}


if(Max_a>0)
{
*SproutTips(i_max,j_max) = 1;
}

}									
																															
					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 } 





}

/*
void CellVesselStructure::GrowSprout(Matrix& Signal)
{

int I=0;
int J=0;
int i=0;
int j=0;
int k=0;
int original_length = CellListLength;
double v_l=0;
double v_r=0;
double v_u=0;
double v_d=0;
double vx=0;
double vy=0;
double v=0;
double mu = 1000;
extern int Xnodes, Ynodes;
bool grow = 0;
bool tgrow = 0;
    
	VesselCell* pCurrentVesselCell =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell3 =  pLastVesselCell;
	
	VesselCell* pNewVesselCell;
	VesselCell* pTempVesselCell;
	
	

 for(int kk=0; kk<original_length; kk++)
 {
	i = pCurrentVesselCell->i;
    j = pCurrentVesselCell->j;
 
    
	if(pCurrentVesselCell->cFate == 'T'&& (i>1)&&(i<Xnodes-2)&&(j>1)&&(j<Ynodes-2))
   {
	//cout<<"enter sprouttip loop"<<endl;
	    		
		tgrow = 0;
		
		
	v_l = mu*(*Signal(i-1,j)-*Signal(i,j));
	v_r = mu*(*Signal(i+1,j)-*Signal(i,j));
	v_d = mu*(*Signal(i,j-1)-*Signal(i,j));
	v_u = mu*(*Signal(i,j+1)-*Signal(i,j));
		
	
	if(v_l > 0 && v_r > 0)
	{
	
	if(v_l > v_r)
	{
	vx = -v_l;
	}
	else
	{
	vx = v_r;
	}
	
	}
	else if(v_l > 0 && v_r <= 0)
	{
	vx = -v_l;
	}
	else if(v_l <= 0 && v_r > 0)
	{
	vx = v_r;
	}
	else
	{
	vx = 0;
	}
	
	
	if(v_d > 0 && v_u > 0)
	{
	
	if(v_d > v_u)
	{
	vy = -v_d;
	}
	else
	{
	vy = v_u;
	}
	
	}
	else if(v_d > 0 && v_u <= 0)
	{
	vy = -v_d;
	}
	else if(v_d <= 0 && v_u > 0)
	{
	vy = v_u;
	}
	else
	{
	vy = 0;
	}
	
//do two times of three situation (1) taken and connected (2) taken and unconnected (3)not taken

//

   if(min_abs(vx) > 0 && min_abs(vy) > 0)
	{
	
	if(min_abs(vx) > min_abs(vy))
	{
	if(vx>0)
	{
	I = i+1;
	J = j;
	}
	else
	{
	I = i-1;
	J = j;
	}
	
	if(*Indicator(I,J)==1)//taken
	{
	pCurrentVesselCell1 = FindCell(I,J);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	else//
	{
	if(vy>0)
	{
	I = i;
	J = j+1;
	}
	else
	{
	I = i;
	J = j-1;
	}
	
	if(*Indicator(I,J)==1)//taken
	{
	pCurrentVesselCell1 = FindCell(I,J);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}
	else//vacant
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 pCurrentVesselCell2-> cFate = 'S';
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = I ;
	  pCurrentVesselCell3->j = J ;
      pCurrentVesselCell3->cIndex = CellListLength;
	   CellListLength++;
	  *Indicator(I,J) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	Connect(pCurrentVesselCell , pCurrentVesselCell3);
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	
	}
	
	}
	}
	else//vacant
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 pCurrentVesselCell2-> cFate = 'S';
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = I ;
	  pCurrentVesselCell3->j = J ;
      pCurrentVesselCell3->cIndex = CellListLength;
	   CellListLength++;
	  *Indicator(I,J) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	Connect(pCurrentVesselCell , pCurrentVesselCell3);
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	}
	
	
	}//vx>vy
	else//vy>vx
	{
	if(vy>0)
	{
	I = i;
	J = j+1;
	}
	else
	{
	I = i;
	J = j-1;
	}
	
	if(*Indicator(I,J)==1)//taken
	{
	pCurrentVesselCell1 = FindCell(I,J);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	else
	{
	if(vx>0)
	{
	I = i+1;
	J = j;
	}
	else
	{
	I = i-1;
	J = j;
	}
	
	if(*Indicator(I,J)==1)//taken
	{
	pCurrentVesselCell1 = FindCell(I,J);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}
	else//vacant
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 pCurrentVesselCell2-> cFate = 'S';
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = I ;
	  pCurrentVesselCell3->j = J ;
      pCurrentVesselCell3->cIndex = CellListLength;
	   CellListLength++;
	  *Indicator(I,J) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	Connect(pCurrentVesselCell , pCurrentVesselCell3);
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	
	}

	
	}
	}
	else//vacant
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 pCurrentVesselCell2-> cFate = 'S';
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = I ;
	  pCurrentVesselCell3->j = J ;
      pCurrentVesselCell3->cIndex = CellListLength;
	   CellListLength++;
	  *Indicator(I,J) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	Connect(pCurrentVesselCell , pCurrentVesselCell3);
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	
	}

	}
	
	}//two directions both probable
	
	else if(min_abs(vx) > 0 && min_abs(vy) <= 0)
	{
	if(vx>0)
	{
	I = i+1;
	J = j;
	}
	else
	{
	I = i-1;
	J = j;
	}
	
	
	if(*Indicator(I,J)==1)//taken
	{
	pCurrentVesselCell1 = FindCell(I,J);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	
	}
	else//vacant
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 pCurrentVesselCell2-> cFate = 'S';
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = I ;
	  pCurrentVesselCell3->j = J ;
      pCurrentVesselCell3->cIndex = CellListLength;
	   CellListLength++;
	  *Indicator(I,J) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	Connect(pCurrentVesselCell , pCurrentVesselCell3);
	grow=1;
	 }//if there is proliferation
	 k++;
	
	//while sweep neibour
	
	}

	
	}

	
	}//only one direction
	else if(min_abs(vx)<=0 && min_abs(vy)> 0)
	{
	
	if(vy>0)
	{
	I = i;
	J = j+1;
	}
	else
	{
	I = i;
	J = j-1;
	}
	
	if(*Indicator(I,J)==1)//taken
	{
	pCurrentVesselCell1 = FindCell(I,J);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}
	else//vacant
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 pCurrentVesselCell2-> cFate = 'S';
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = I ;
	  pCurrentVesselCell3->j = J ;
      pCurrentVesselCell3->cIndex = CellListLength;
	   CellListLength++;
	  *Indicator(I,J) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	Connect(pCurrentVesselCell , pCurrentVesselCell3);
	grow=1;
	 }//if there is proliferation
	 k++;
	
	//while sweep neibour
	
	}

	
	}

	
	}//only one direction
	
	
	//
	}//if tip cell
					


					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }//while sweep

	
	pLastVesselCell = pCurrentVesselCell3;
			


 }
 


void CellVesselStructure::Grow_One(Matrix& Signal)//take place step by step
{

int i=0;
int j=0;
int k=0;
int original_length = CellListLength;
double v_l=0;
double v_r=0;
double v_u=0;
double v_d=0;
double vx=0;
double vy=0;
double v=0;
double mu = 1000;
extern int Xnodes, Ynodes;
bool grow = 0;
    
	VesselCell* pCurrentVesselCell =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell3 =  pLastVesselCell;
	
	VesselCell* pNewVesselCell;

 for(int kk=0; kk<original_length; kk++)
 {
 



   if(pCurrentVesselCell->cFate == 'T')
   {
   	 
   	i = pCurrentVesselCell->i;
    j = pCurrentVesselCell->j;
	v_l = mu*(*Signal(i-1,j)-*Signal(i,j));
	v_r = mu*(*Signal(i+1,j)-*Signal(i,j));
	v_d = mu*(*Signal(i,j-1)-*Signal(i,j));
	v_u = mu*(*Signal(i,j+1)-*Signal(i,j));
		
	
	if(v_l > 0 && v_r > 0)
	{
	
	if(v_l > v_r)
	{
	vx = -min_Round(v_l);
	}
	else
	{
	vx = min_Round(v_r);
	}
	
	}
	else if(v_l > 0 && v_r <= 0)
	{
	vx = -min_Round(v_l);
	}
	else if(v_l <= 0 && v_r > 0)
	{
	vx = min_Round(v_r);
	}
	else
	{
	vx = 0;
	}
	
	
	if(v_d > 0 && v_u > 0)
	{
	
	if(v_d > v_u)
	{
	vy = -min_Round(v_d);
	}
	else
	{
	vy = min_Round(v_u);
	}
	
	}
	else if(v_d > 0 && v_u <= 0)
	{
	vy = -min_Round(v_d);
	}
	else if(v_d <= 0 && v_u > 0)
	{
	vy = min_Round(v_u);
	}
	else
	{
	vy = 0;
	}

	
	
	//cout<<"vx: "<<vx<<endl;
	//cout<<"vy: "<<vy<<endl;
	
	
	if(*Indicator(i,j)==1)
	{
	pCurrentVesselCell1 = FindCell(i,j);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}
	else if(*Indicator(i,j)==0)
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	
	//cout<<"k"<<k<<"   cIndex"<<pCurrentVesselCell->cIndex;
	//getchar();
	
	//cout<<"point to"<<pCurrentVesselCell->cConnectIndex[k];
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	//cout<<"point to"<<pCurrentVesselCell2->cIndex;
	//getchar();
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	  	  
	

	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = i ;
	  pCurrentVesselCell3->j = j ;
      pCurrentVesselCell3->cIndex = CellListLength ;
	   CellListLength++;
	  *Indicator(i,j) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	 //pCurrentVesselCell3->cConnection = 1;

	 // pCurrentVesselCell3->cConnectIndex[0] = pCurrentVesselCell->cIndex;
	 Connect(pCurrentVesselCell , pCurrentVesselCell3);
	 
	 
   
	
	
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	
	
	//v=pow((pow(vx,2)+pow(vy,2)),0.5);
	 

	}//if vacant
	
   }//if tip cell
					



					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }//while sweep

	
	pLastVesselCell = pCurrentVesselCell3;
			
	
 }
void CellVesselStructure::Grow_zero(Matrix& Signal)//take place only
{

int i=0;
int j=0;
int k=0;
int original_length = CellListLength;
double vx=0;
double vy=0;
double v=0;
double mu = 1000;
extern int Xnodes, Ynodes;
bool grow = 0;
    
	VesselCell* pCurrentVesselCell =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell3 =  pLastVesselCell;
	
	VesselCell* pNewVesselCell;

 for(int kk=0; kk<original_length; kk++)
 {
 



   if(pCurrentVesselCell->cFate == 'T')
   {
   	 
   	i = pCurrentVesselCell->i;
    j = pCurrentVesselCell->j;
	vx = min_Round(mu*(*Signal(i+1,j)-*Signal(i-1,j)));
	vy = min_Round(mu*(*Signal(i,j+1)-*Signal(i,j-1)));
	//cout<<"vx: "<<vx<<endl;
	//cout<<"vy: "<<vy<<endl;
	i=i+vx;
	j=j+vy;
	
	if(*Indicator(i,j)==1)
	{
	pCurrentVesselCell1 = FindCell(i,j);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}
	else if(*Indicator(i,j)==0)
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	
	//cout<<"k"<<k<<"   cIndex"<<pCurrentVesselCell->cIndex;
	//getchar();
	
	//cout<<"point to"<<pCurrentVesselCell->cConnectIndex[k];
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	//cout<<"point to"<<pCurrentVesselCell2->cIndex;
	//getchar();
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	  	  
	

	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = i ;
	  pCurrentVesselCell3->j = j ;
      pCurrentVesselCell3->cIndex = CellListLength ;
	   CellListLength++;
	  *Indicator(i,j) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	 //pCurrentVesselCell3->cConnection = 1;

	 // pCurrentVesselCell3->cConnectIndex[0] = pCurrentVesselCell->cIndex;
	 Connect(pCurrentVesselCell , pCurrentVesselCell3);
	 
	 
   
	
	
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	
	
	//v=pow((pow(vx,2)+pow(vy,2)),0.5);
	 

	}//if vacant
	
   }//if tip cell
					



					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }//while sweep

	
	pLastVesselCell = pCurrentVesselCell3;
			
	
 }
void CellVesselStructure::Grow_Deprecate(Matrix& Signal)//take place only
{

int i=0;
int j=0;
int k=0;
int original_length = CellListLength;
double vx=0;
double vy=0;
double v=0;
double mu = 1000;
extern int Xnodes, Ynodes;
bool grow = 0;
    
	VesselCell* pCurrentVesselCell =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell3 =  pLastVesselCell;
	
	VesselCell* pNewVesselCell;

 for(int kk=0; kk<original_length; kk++)
 {
 



   if(pCurrentVesselCell->cFate == 'T')
   {
   	 
   	i = pCurrentVesselCell->i;
    j = pCurrentVesselCell->j;
	vx = min_Round(mu*(*Signal(i+1,j)-*Signal(i-1,j)));
	vy = min_Round(mu*(*Signal(i,j+1)-*Signal(i,j-1)));
	//cout<<"vx: "<<vx<<endl;
	//cout<<"vy: "<<vy<<endl;
	i=i+vx;
	j=j+vy;
	
	if(*Indicator(i,j)==1)
	{
	pCurrentVesselCell1 = FindCell(i,j);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}
	else if(*Indicator(i,j)==0)
	{  
	grow = 0;
	k=0;

	
	while(k< pCurrentVesselCell->cConnection && grow==0)
	{
	
	//cout<<"k"<<k<<"   cIndex"<<pCurrentVesselCell->cIndex;
	//getchar();
	
	//cout<<"point to"<<pCurrentVesselCell->cConnectIndex[k];
	  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	//cout<<"point to"<<pCurrentVesselCell2->cIndex;
	//getchar();
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	  	  
	

	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = i ;
	  pCurrentVesselCell3->j = j ;
      pCurrentVesselCell3->cIndex = CellListLength ;
	   CellListLength++;
	  *Indicator(i,j) = 1;
	  	  pCurrentVesselCell3->cSetPressure = 0;
	 //pCurrentVesselCell3->cConnection = 1;

	 // pCurrentVesselCell3->cConnectIndex[0] = pCurrentVesselCell->cIndex;
	 Connect(pCurrentVesselCell , pCurrentVesselCell3);
	 
	 
   
	
	
	grow=1;
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
	
	
	//v=pow((pow(vx,2)+pow(vy,2)),0.5);
	 

	}//if vacant
	
   }//if tip cell
					



					
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }//while sweep

	
	pLastVesselCell = pCurrentVesselCell3;
			
	
 }
*/ 

/*
 void CellVesselStructure::String_Interact(Matrix& Signal)
{
	int i=0;
	int j=0;
	
	bool all_trapped = 0;
	double tol = 2;
	double error = 10;
	double vx=0;
    double vy=0;
    double v=0;
    double mu = 1000;
	double sticky = 1;
	int Index = 0;
	double deltax = 0;
	double deltay = 0;
	
	VesselCell* pCurrentVesselCell = pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 = pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 = pFirstVesselCell;
	
	while(pCurrentVesselCell)
	{
	if(pCurrentVesselCell->cFate == 'T')
	{
	
	
	i = pCurrentVesselCell->i;
    j = pCurrentVesselCell->j;
	vx = min_Round(mu*(*Signal(i+1,j)-*Signal(i-1,j)));
	vy = min_Round(mu*(*Signal(i,j+1)-*Signal(i,j-1)));
	//cout<<"vx: "<<vx<<endl;
	//cout<<"vy: "<<vy<<endl;
	i=i+vx;
	j=j+vy;
	
	if(*Indicator(i,j)==1)
	{
	pCurrentVesselCell1 = FindCell(i,j);
	if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	{
	Connect(pCurrentVesselCell,pCurrentVesselCell1);
	}
	}//anastmosis
	else
	{
	pCurrentVesselCell->i=i;
	pCurrentVesselCell->j=j;
	
	}
		
	
	}
	
	
	
	
	pCurrentVesselCell = pCurrentVesselCell-> pNextVesselCell;
	}

	
	int round =0;
	while(!all_trapped&& round<cConnectRange-1 )
	{
    round++;
	all_trapped = 1;
	cout<<"error:  "<<error<<endl;
	getchar();
	//error = 0;
	VesselCell* pCurrentVesselCell = pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 = pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 = pFirstVesselCell;
	
       while(pCurrentVesselCell)
       {
 
	cout<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;
	getchar();
			if(pCurrentVesselCell->cPericytes==0&&pCurrentVesselCell->cFate != 'D'&&pCurrentVesselCell->cFate != 'T')
			{
		
		
			vx = 0;
			vy = 0;
			
			 for(int k=0; k< pCurrentVesselCell-> cConnection; k++)
             {
					Index = pCurrentVesselCell-> cConnectIndex[k];
					pCurrentVesselCell2 = FindCell(Index);
					deltax = sticky*(pCurrentVesselCell2->i - pCurrentVesselCell->i );
					deltay = sticky*(pCurrentVesselCell2->j - pCurrentVesselCell->j );
					vx = vx + deltax;
					cout<<"vx"<<vx<<endl;

					vy = vy + deltay;
			
			       cout<<"vy"<<vy<<endl;
			}
			
			/*
			vx=(vx);
							if(min_abs(vx)>error)
			{
			error =min_abs(vx);
			}
			vy=(vy);
				
			if(min_abs(vy)>error)
			{
			error = min_abs(vy);
			}
			
			

			// pCurrentVesselCell->i = pCurrentVesselCell->i + vx;
			// pCurrentVesselCell->j = pCurrentVesselCell->j + vy;
			
			if(vx>0.5)
			{
			
			i = pCurrentVesselCell->i + 1;

			}
			else if (vx<-0.5)
			{
			i = pCurrentVesselCell->i - 1;
			}
			
			if(vy>0.5)
			{
			j = pCurrentVesselCell->j + 1;

			}
			else if (vy<-0.5)
			{
			j = pCurrentVesselCell->j - 1;
			}
			
			if(i == pCurrentVesselCell->i&&j == pCurrentVesselCell->j)
			{
			 *Indicator(pCurrentVesselCell->i,pCurrentVesselCell->j)=1;
			}
			else
			{
		
			 if(*Indicator(i,j)==1)
	          {
			  
	          pCurrentVesselCell1 = FindCell(i,j);
	           if(!Connected(pCurrentVesselCell,pCurrentVesselCell1))
	           {
	           Connect(pCurrentVesselCell,pCurrentVesselCell1);
	           }
			   
			  }
			  else
			  {
			  
			  all_trapped=0;
			  *Indicator(pCurrentVesselCell->i,pCurrentVesselCell->j)=0;
			  pCurrentVesselCell->i=i;
			  pCurrentVesselCell->j=j;
			  *Indicator(pCurrentVesselCell->i,pCurrentVesselCell->j)=1;

			  }
			  
			//  cout<<"This P or S move to"<<pCurrentVesselCell->cIndex<<"   "<<pCurrentVesselCell->cFate<<"   "<<pCurrentVesselCell->i<<"   "<<pCurrentVesselCell->j<<endl;
	          //getchar();
          }

		
        	
			}//no pericytes and alive
  			
					
									
					
         pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
	   } 
 
 
    }

}
**/

/*
 void CellVesselStructure::Grow(Matrix& Signal)//in between only
{

int i=0;
int j=0;
int k=0;
int original_length = CellListLength;

double v=0;
double mu = 1000;
extern int Xnodes, Ynodes;
bool grow = 0;
    
	VesselCell* pCurrentVesselCell = pFirstVesselCell;
	VesselCell* pCurrentVesselCell1 =pFirstVesselCell;
	VesselCell* pCurrentVesselCell2 =  pFirstVesselCell;
	VesselCell* pCurrentVesselCell3 = pLastVesselCell;
	
	VesselCell* pNewVesselCell;

 for(int kk=0; kk<original_length; kk++)
 {
 
   if(pCurrentVesselCell->cFate == 'T')
   {
   	 
   	i = pCurrentVesselCell->i;
    j = pCurrentVesselCell->j;
	k=0;
	while(k< pCurrentVesselCell->cConnection)
	{   
		  pCurrentVesselCell2 = FindCell(pCurrentVesselCell->cConnectIndex[k]);
	cout<<"point to"<<pCurrentVesselCell2->cIndex;
	getchar();
	
	if(pCurrentVesselCell2-> cFate == 'P')
	 {
	 
	  pNewVesselCell = new VesselCell;
	  pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell3;
	
	  pCurrentVesselCell3->pNextVesselCell = pNewVesselCell;
	   
	  pCurrentVesselCell3 = pCurrentVesselCell3->pNextVesselCell;
	  pCurrentVesselCell3->i = pCurrentVesselCell2->i ;
	  pCurrentVesselCell3->j = pCurrentVesselCell2->j ;
      pCurrentVesselCell3->cIndex = CellListLength ;
	   CellListLength++;
	
	  	  pCurrentVesselCell3->cSetPressure = 0;
	 //pCurrentVesselCell3->cConnection = 1;

	 // pCurrentVesselCell3->cConnectIndex[0] = pCurrentVesselCell->cIndex;
	 cout<<"before"<<endl;
	 DisConnect(pCurrentVesselCell,pCurrentVesselCell2);
	  cout<<"after"<<endl;
	 Connect(pCurrentVesselCell , pCurrentVesselCell3);
	 Connect(pCurrentVesselCell2 , pCurrentVesselCell3);
    pCurrentVesselCell2-> cFate = 'S';
	pCurrentVesselCell3-> cFate = 'S';
	cout<<"Become"<<pCurrentVesselCell2->cIndex;
	cout<<"Become"<<pCurrentVesselCell3->cIndex;
	getchar();
	
	
	 }//if there is proliferation
	 k++;
	
	}//while sweep neibour
		
   }//if tip cell
	
 pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;					
 }//for sweep

	
	pLastVesselCell = pCurrentVesselCell3;
			
	
 }

 
 **/
 
 CellVesselStructure* CellVesselStructure::GetAddress(void)
 {
 return this;
 }
 
 VesselCell* CellVesselStructure::FindCell(int Index)
{
VesselCell* pCurrentVesselCell = pFirstVesselCell;
pCurrentVesselCell = pCurrentVesselCell-> pNextVesselCell;
while(pCurrentVesselCell && pCurrentVesselCell->cIndex != Index)
{
pCurrentVesselCell = pCurrentVesselCell-> pNextVesselCell;
}

if(pCurrentVesselCell)
{
return pCurrentVesselCell;
}
else
{
return pFirstVesselCell;
}

}

 VesselCell* CellVesselStructure::FindCell(int i, int j)
{

bool Found = 0;

VesselCell* pCurrentVesselCell = pFirstVesselCell;
//cout<<pCurrentVesselCell->cIndex<<endl;

while(pCurrentVesselCell && !Found)
{
//cout<<pCurrentVesselCell->cIndex<<endl;
pCurrentVesselCell = pCurrentVesselCell-> pNextVesselCell;
for(int ii=0; ii<pCurrentVesselCell->cCover; ii++)
{
if(pCurrentVesselCell->i[ii]==i && pCurrentVesselCell->j[ii]==j)
{
	Found = 1;
}
//cout<<"here2"<<endl;
}




//cout<<"here1"<<endl;
}

 

if(Found == 1)
{
return pCurrentVesselCell;
}
else
{
return pFirstVesselCell;
}


}




 void CellVesselStructure::ColorCell(void)
{
extern Matrix CellColor;
int i = 0;
int j = 0;
extern int Xnodes;
extern int Ynodes;
CellColor = zeros(Xnodes, Ynodes);

VesselCell* pCurrentVesselCell = pFirstVesselCell;
pCurrentVesselCell = pCurrentVesselCell->pNextVesselCell;
while(pCurrentVesselCell)
{
if(pCurrentVesselCell->cFate == 'T')
{
   for(int ii=0; ii<pCurrentVesselCell->cCover; ii++)
   {
	i = pCurrentVesselCell->i[ii];
	j = pCurrentVesselCell->j[ii];
	
	*CellColor(i,j) = 80;
   }
}
else if(pCurrentVesselCell->cFate == 'S')
{
for(int ii=0; ii<pCurrentVesselCell->cCover; ii++)
   {
	i = pCurrentVesselCell->i[ii];
	j = pCurrentVesselCell->j[ii];
	
	*CellColor(i,j) = 10;//stable
	
   }

}
else if(pCurrentVesselCell->cFate == 'P')
{

for(int ii=0; ii<pCurrentVesselCell->cCover; ii++)
   {
	i = pCurrentVesselCell->i[ii];
	j = pCurrentVesselCell->j[ii];
	
	*CellColor(i,j) = 50;//Proliferation
	
   }
}





pCurrentVesselCell = pCurrentVesselCell-> pNextVesselCell;
}

}

bool Connected(VesselCell* a, VesselCell* b) 
{
for(int j=0;j<(a->cConnection);j++)
{
if(a->cConnectIndex[j]==b->cIndex)
{
 return 1;
}
}

return 0;

}



void Connect(VesselCell* a, VesselCell* b)
{
int i = a->cConnection;

if(i<a->cConnectRange)
{
a->cConnectIndex[i] = b->cIndex;
}

a->cConnection = a->cConnection + 1;
//cout<<a->cConnection<<endl;
i=b->cConnection;
if(i<a->cConnectRange)
{
b->cConnectIndex[i] = a->cIndex;
}
b->cConnection = b->cConnection + 1;
//cout<<b->cConnection<<endl;
}


void DisConnect(VesselCell* a, VesselCell* b)
{
int i = b->cIndex;
int j=0;
while(a->cConnectIndex[j]!=i&&j<a->cConnectRange-1)
{
j++;
}

if(j==(a->cConnectRange-1))
{
  a->cConnectIndex[j]=0;
}
else
{
  while(j<a->cConnectRange-1)
   {
   a->cConnectIndex[j]=a->cConnectIndex[j+1];
   j++;
   }
  a->cConnectIndex[j]=0;
}

a->cConnection = a->cConnection-1;

 i = a->cIndex;
 j=0;
while(b->cConnectIndex[j]!=i&&j<b->cConnectRange-1)
{
j++;
}

if(j==b->cConnectRange-1)
{
  b->cConnectIndex[j]=0;
}
else
{
  while(j<b->cConnectRange-1)
   {
   b->cConnectIndex[j]=b->cConnectIndex[j+1];
   j++;
   }
  b->cConnectIndex[j]=0;
}

b->cConnection = b->cConnection-1;



}

void RemoveUnConnectedPoint(VesselCell* a, int i, int j)
{
  
  int index = 0;
  if(a->cCover > 1)
  {
  while(a->i[index]!=i||a->j[index]!=j)
		{
		index++;
		}
	if(index == (a->cCover - 1))
	{
		a->cCover = a->cCover - 1;
	}
	else
	{	
		while(index < (a->cCover - 1))
		{
			a->i[index] = a->i[index+1];
			a->j[index] = a->j[index+1];
			index++;
		}
		a->cCover = a->cCover - 1;
	}
	
	   
  }
 	  
  
   
}



void CompeteFate(VesselCell* a, VesselCell* b, Matrix& Signal, char Win, char Lose, char Fair)
{


int i1=0;
int j1=0;
int i2=0;
int j2=0;

double Max_a = 0;
double Max_b = 0;
/*
cout<<a->cIndex;
cout<<endl;
cout<<b->cIndex;
getchar();
*/
if(a->cFate == Win && b->cFate == Win)
{
for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
if(*Signal(i1,j1)>Max_a)
{
Max_a = *Signal(i1,j1);
//cout<< Max_a<<"  ";
}

}

for(int ii=0; ii < b->cCover; ii++)
{
i2 = b->i[ii];
j2 = b->j[ii];
if(*Signal(i2,j2)>Max_b)
{
Max_b = *Signal(i2,j2);
//cout<< Max_b<<endl;

}

}


if(Max_a>Max_b)
{
a->cFate = Win;
b->cFate = Lose;

}
else if(Max_a==Max_b)
{
a->cFate = Fair;
b->cFate = Fair;

}
else
{

a->cFate = Lose;
b->cFate = Win;

}


//cout<<a->cFate;
//cout<<endl;
//cout<<b->cFate;
//getchar();


}
//cout<<a->cIndex<<"and"<<b->cIndex<<endl;
//cout<<a->cFate<<"and"<<b->cFate<<endl;


}

void SetTipFate(VesselCell* a, Matrix& Signal, char Win, char Lose, double Threshold)
{
int i1=0;
int j1=0;
double Max_a = 0;
bool coated = 0;
extern Matrix Stability;
extern Matrix TAF;
extern Matrix Wss;
extern int Xnodes,Ynodes;
double Stable_up = RetrieveDoubleParameter( ParameterFile , "Stable_up");
double Stable_down = RetrieveDoubleParameter( ParameterFile , "Stable_down");
       
	
   
      
for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
//cout<<*TAF(i1,j1);
double RandNumber = float(rand())/float(RAND_MAX);
if(RandNumber<*Stability(i1,j1))
{
 coated = 1;
}

if(*Signal(i1,j1)>Max_a)
{
Max_a = *Signal(i1,j1);
}

}
/*
if(Max_a>Threshold&&(!coated))
{
a->cFate = Win;
//cout<<Max_a<<"and"<<Threshold;
}
else
{
a->cFate = Lose;
//cout<<Lose;
//getchar();
}
*/

double RandNumber = float(rand())/float(RAND_MAX);

if(RandNumber<Max_a)
{

for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
//cout<<*TAF(i1,j1);
*Stability(i1,j1) = *Stability(i1,j1) - AngioDeltaT * Stable_down;
}
if(!coated)
{
a->cFate = Win;
}
else
{
a->cFate = Lose;
}
//cout<<Max_a<<"and"<<Threshold;
}
else
{
a->cFate = Lose;
//cout<<Lose;
//getchar();
for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
//cout<<*TAF(i1,j1);
*Stability(i1,j1) = *Stability(i1,j1) + AngioDeltaT * Stable_up;
}

}


}


void SetTipFate(VesselCell* a, Matrix& Signal, char Win, char Lose)
{
int i1=0;
int j1=0;
double Max_a = 0;


for(int ii=0; ii < a->cCover; ii++)
{
i1 = a->i[ii];
j1 = a->j[ii];
//cout<<*TAF(i1,j1);
if(*Signal(i1,j1)>Max_a)
{
Max_a = *Signal(i1,j1);
}

}


if(Max_a)
{
a->cFate = Win;
//cout<<Max_a<<"and"<<Threshold;
}
else
{
a->cFate = Lose;
//cout<<Lose;
//getchar();
}




}




/**********************************hualidefeigexian**********************************************hualidefeigexian*********************************************/





/*
class AngioSolverPoint{
public:
 int i;
 int j;
 AngioSolverPoint* pNextSolverPoint;
AngioSolverPoint();
};

AngioSolverPoint::AngioSolverPoint()
{ i=0; j=0; pNextSolverPoint = NULL; }



*/


VesselPoint::VesselPoint()
{ 
	i=0;
	j=0; 
	AmIArtery = 0;
	AmIVenous = 0;
	MyAge = 0;
	i_origin = 0;
	j_origin = 0;
	pNextVesselPoint = NULL; 
	pPreviousVesselPoint = NULL;
}

VesselPoint::~VesselPoint()
{
cout<<"destructor";
}

VesselStructure::VesselStructure()
{
	pFirstVesselPoint = NULL;
	pLastVesselPoint = NULL;
	NodeLength = 0;
	CellNumber = 0;
	SproutNumber = 0;
	VesselListLength = 0;
	//cout <<"function VesselStructure running"<<endl;	
}

VesselStructure::~VesselStructure()
{
//cout <<"function ~VesselStructure running"<<endl;	
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 if( pCurrentVesselPoint)
 {
	while(pCurrentVesselPoint)
	{
		pCurrentVesselPoint = pCurrentVesselPoint ->pNextVesselPoint;
		delete  pCurrentVesselPoint->pPreviousVesselPoint;
	}
	delete  pCurrentVesselPoint;
 }
}




int min_Round( double input )
{
if(input>0)
{

  return ImprovedRound(input); 
 }
 else if(input<0)
 {
  return -1*ImprovedRound(-input); 
 }
 else
 {
 return 0;
 }
}


double min_abs(double x)
{
if(x<0) 
{return -x;}
return x;
}


void VesselStructure::VesselInitialization_R( bool VesselResume )
{
 
 //cout <<"function ~VesselInitialization running"<<endl;	
 extern Matrix X, Y;
 extern int Xnodes, Ynodes;
 extern Matrix BloodVesselIndicator;
 extern Matrix BloodVesselIndicatorX;
 extern Matrix BloodVesselIndicatorY;
 extern Matrix InterPressure;
 extern Matrix CellColor;
 
 extern Matrix BloodVesselRadius;
 //mextern Matrix BloodVesselFlowRate;
 extern Matrix HaematocritLevel;
 extern Matrix SproutTips;
 extern Matrix SproutAge;
 extern Matrix VesselAge;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodNodePressure;
 extern Matrix BloodVesselPressureX;
 //extern Matrix DirectionX, DirectionY;
 extern Matrix FlowX, FlowY;
 extern Matrix WssX, WssY;
 extern int innumbers, outnumbers;
 extern bool Injectionrandomized;
 Injectionrandomized = bool(RetrieveDoubleParameter( ParameterFile , "Injectionrandomized"));
 
 innumbers = int(RetrieveDoubleParameter( ParameterFile , "innumbers"));
 outnumbers = int(RetrieveDoubleParameter( ParameterFile , "outnumbers"));
 InterPressure=zeros(Xnodes,Ynodes);
 CellColor = zeros(Xnodes, Ynodes);
 
 
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern Matrix WallGradient;
  WallGradient=zeros(Xnodes, Ynodes);
 NodeLength = 1e-5;//*X(1) - *X(0);
 
  
 this->InitializeTime( VesselResume );
 //InitialAngiogenesisBackground(); //substituting DoSomeInitialisation()
// this->InitializeVesselListInOrder( VesselResume );

 if(!VesselResume)
 {
  cout<<"ramdon"<<Injectionrandomized<<"in"<<innumbers<<"out"<<outnumbers<<endl;
 this->InjectPosition(innumbers, outnumbers, Injectionrandomized);
cout<<"here after"<<endl;
 }
  
 
 
 this->InitializeVesselMatrix_R( VesselResume );

int Iteration = 5000000;//5000000;
double Tolerance = 0.0001;//0.01;
double MaxConduct;
 double Pin, Pout;
Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
Pout = RetrieveDoubleParameter( ParameterFile , "Pout");

BloodNodePressure = zeros(Xnodes, Ynodes);
UpdateViscosity();

cout<<"Check"<<endl; 

 
UpdateConductivity();

{
//ChainAngioPressureUpdate(Iteration, Tolerance);
ChainAngioPressureUpdate(Iteration, Tolerance); 
//ChainAngioPressureUpdate(Iteration, Tolerance);

GetVesselPressureAndFlow(Tolerance);
UpdateWallShearStress();
SteadyStateHematocrit();


UpdateRadius();
	
	
}

TransOutPut();
 RegenerateFlow();
  //cout <<"function ~VesselInitialization finished"<<endl;	
}
void VesselStructure::VesselInitialization( bool VesselResume )
{
	
	//cout <<"function ~VesselInitialization running"<<endl;	
	extern Matrix X, Y;
	extern int Xnodes, Ynodes;
	extern Matrix BloodVesselIndicator;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix InterPressure;
	extern Matrix CellColor;
	
	extern Matrix BloodVesselRadius;
	//mextern Matrix BloodVesselFlowRate;
	extern Matrix HaematocritLevel;
	extern Matrix SproutTips;
	extern Matrix SproutAge;
	extern Matrix VesselAge;
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodNodePressure;
	extern Matrix BloodVesselPressureX;
	extern Matrix BloodVesselPressureY;
	//extern Matrix DirectionX, DirectionY;
	extern Matrix FlowX, FlowY;
	extern Matrix WssX, WssY;
	extern int innumbers, outnumbers;
	extern bool Injectionrandomized;
	Injectionrandomized = bool(RetrieveDoubleParameter( ParameterFile , "Injectionrandomized"));
	
	innumbers = int(RetrieveDoubleParameter( ParameterFile , "innumbers"));
	outnumbers = int(RetrieveDoubleParameter( ParameterFile , "outnumbers"));
	InterPressure=zeros(Xnodes,Ynodes);
	CellColor = zeros(Xnodes, Ynodes);
	
	
	extern Matrix VisX, VisY; //viscosity
	extern Matrix ConductX, ConductY; //conductivity
	extern Matrix WallGradient;
	WallGradient=zeros(Xnodes, Ynodes);
	NodeLength = 1e-5;//*X(1) - *X(0);
	
	
	this->InitializeTime( VesselResume );
	//InitialAngiogenesisBackground(); //substituting DoSomeInitialisation()
	// this->InitializeVesselListInOrder( VesselResume );
	
	if(!VesselResume)
	{
		cout<<"ramdon"<<Injectionrandomized<<"in"<<innumbers<<"out"<<outnumbers<<endl;
		this->InjectPosition(innumbers, outnumbers, Injectionrandomized);
		cout<<"here after"<<endl;
	}
	
	
	
	this->InitializeVesselMatrix( VesselResume );
	
	int Iteration = 5000000;//5000000;
	double Tolerance = 0.0001;//0.01;
	
	double Pin, Pout;
	Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
	Pout = RetrieveDoubleParameter( ParameterFile , "Pout");
	
	
	ChainAngioPressureUpdate(Iteration, Tolerance); 
	cout<<"Check"<<endl; 
	
	GetVesselPressureAndFlow(Tolerance);
	//cout <<"function ~VesselInitialization finished"<<endl;	
}
void VesselStructure::InjectPosition(int innumbers, int outnumbers, bool randomized)
{
	
	
	
	extern int Xnodes;
	extern int Ynodes;
    int i = 0;
	int j = (Ynodes - 1)/2;
	int head = 90;
	int tail = 110;
	
	
	
	//cout<<"  2 ";
	if(!randomized)
	{
		VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
		
		int inflowX=0, inflowY=Ynodes-1;
		int outflowX=Xnodes-1, outflowY=Ynodes-1;
		
		/*
		 while(pCurrentVesselPoint)
		 {
		 if(pCurrentVesselPoint->i==head&&pCurrentVesselPoint->j==j)
		 {
		 pCurrentVesselPoint->AmIArtery=1;
		 }
		 else if(pCurrentVesselPoint->i==tail-1&&pCurrentVesselPoint->j==j)
		 {
		 pCurrentVesselPoint->AmIVenous=1;
		 }
		 pCurrentVesselPoint=pCurrentVesselPoint->pNextVesselPoint;
		 }
		 */
		
		
		while(pCurrentVesselPoint)
		{
			if((pCurrentVesselPoint->i==0&&pCurrentVesselPoint->j==0)||(pCurrentVesselPoint->i==0&&pCurrentVesselPoint->j==50)||(pCurrentVesselPoint->i==0&&pCurrentVesselPoint->j==150)||(pCurrentVesselPoint->i==0&&pCurrentVesselPoint->j==200))
			{
				pCurrentVesselPoint->AmIArtery=1;
			}
			else if((pCurrentVesselPoint->i==200&&pCurrentVesselPoint->j==0)||(pCurrentVesselPoint->i==200&&pCurrentVesselPoint->j==50)||(pCurrentVesselPoint->i==200&&pCurrentVesselPoint->j==150)||(pCurrentVesselPoint->i==200&&pCurrentVesselPoint->j==200))
			{
				pCurrentVesselPoint->AmIVenous=1;
			}
			pCurrentVesselPoint=pCurrentVesselPoint->pNextVesselPoint;
		}
		
		
		
		
		
		
		
		
		
		
	}
	else
	{
		int i=0;
		cout<<"ramdon"<<randomized<<"in"<<innumbers<<"out"<<outnumbers<<endl;
		
		
		while(i<innumbers)
		{
			cout<<"ramdon"<<randomized<<"in"<<innumbers<<"out"<<outnumbers<<endl;
			
			
			int stop= int(( float( rand() ) / float( RAND_MAX))*VesselListLength);
			cout<<stop<<"   ";
			VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
			
			for(int j=0;j<stop;j++)
			{
				pCurrentVesselPoint=pCurrentVesselPoint->pNextVesselPoint;
			}
			if( pCurrentVesselPoint->AmIArtery!=1&&pCurrentVesselPoint->AmIVenous!=1)
			{
				pCurrentVesselPoint->AmIArtery=1;
				i++;
			}		
		}
		
		i=0;
		
		while(i<outnumbers)
		{
			
			int stop= int(( float( rand() ) / float( RAND_MAX))*VesselListLength);
			VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
			
			for(int j=0;j<stop;j++)
			{
				pCurrentVesselPoint=pCurrentVesselPoint->pNextVesselPoint;
			}
			if( pCurrentVesselPoint->AmIArtery!=1&&pCurrentVesselPoint->AmIVenous!=1)
			{
				pCurrentVesselPoint->AmIVenous=1;
				i++;
			}		
		}
		
		
		
		
	}//else end
	
}


void VesselStructure::InitializeTime(bool Resume)
{
 
//cout <<"function Initializetime running"<<endl;
 extern double t;
 FlowBegin = (bool) RetrieveDoubleParameter( ParameterFile , "FlowBegin");
 AdaptionBegin = (bool) RetrieveDoubleParameter( ParameterFile , "AdaptionBegin");
 if(t>0)
 {
	 FlowBegin=1;
	 AdaptionBegin=1;
 }
 if( Resume == 1)
 {
 extern char SaveDirectory[1025];
 char temp_string[1024];
 
	 sprintf( temp_string , "%s/angio_resume.dat" , SaveDirectory );
 ifstream angio_resume( temp_string, ios::in );
 angio_resume >> AngioDeltaT;
			 
 cout<<AngioDeltaT<<"   "<<endl;
            
 angio_resume.close();
 }
 else
 {
	AngioDeltaT = 0.05; //THIS IS WHERE I'M NOT SURE OF
	cout<<"AngioDeltaT"<<AngioDeltaT<<endl;
	

	
 }
//cout <<"function Initializetime finished"<<endl; 
}	
void VesselStructure::ArteryPosition()
{   
	//cout <<"function ArteryPosition running"<<endl; 
	
	
	
	extern int Xnodes;
	extern int Ynodes;
	extern Matrix VesselCells;
	extern Matrix SproutTips;
	extern Matrix MapIndicator;
	extern Matrix Tracer, TracerX, TracerY, TracerS, Wss;
	extern Matrix MapVesselCells;
	extern Matrix MapSproutTips;
    extern Matrix MapFlow;
    extern Matrix MapRadius;
	extern Matrix MapPressure;
	extern Matrix VesselAge;
	extern Matrix BloodVesselIndicator, BloodVesselIndicatorX,  BloodVesselIndicatorY;
    
	Wss = zeros(Xnodes, Ynodes);
	Tracer = zeros(Xnodes, Ynodes);
	TracerS = zeros(Xnodes, Ynodes);
	TracerX = zeros(Xnodes, Ynodes);
	TracerY = zeros(Xnodes, Ynodes);
	VesselCells = zeros(Xnodes, Ynodes);
	SproutTips = zeros(Xnodes, Ynodes);	
	MapIndicator = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapVesselCells =  zeros(2*Xnodes-1, 2*Ynodes-1);
	MapSproutTips = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapFlow = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapRadius = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapPressure = zeros(2*Xnodes-1, 2*Ynodes-1);
	VesselAge = zeros(Xnodes,Ynodes);	
	
	
	
	
	
	
    pFirstVesselPoint = new VesselPoint;
	pFirstVesselPoint->pPreviousVesselPoint = NULL;
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	
	
	int i=0;
	int j=0;
	
			
	int i_art = 0;//1-99
	int j_art = 0;//0,100,200
	int i_ven = 101;//101-199
	int j_ven = 50;//50,150	

	j = j_art;
	
	for (i=i_art; i<=49 ; i++)//1-99
    {
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
    }
	
	j = j_art+50;
	
	for (i=i_art; i<=49 ; i++)//1-99
    {
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
    }
	
	j = j_art+150;
	
	for (i=i_art; i<=49 ; i++)//1-99
    {
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
    }


     j = j_art+200;
	
	for (i=i_art; i<=49 ; i++)//1-99
    {
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
    }

	 i_art = 51;
	 j=j_art;
	
	for (i=i_art; i<=150 ; i++)//1-99
    {
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
    }
	
	j = j_art+50;
	
	for (i=i_art; i<=150 ; i++)//1-99
    {
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
    }
	
	j = j_art+150;
	
	for (i=i_art; i<=150 ; i++)//1-99
    {
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
    }


     j = j_art+200;
	
	for (i=i_art; i<=150 ; i++)//1-99
    {
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
    }


	 i_art = 152;
	 j=j_art;
	
	for (i=i_art; i<=199 ; i++)//1-99
    {
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
    }
	
	j = j_art+50;
	
	for (i=i_art; i<=199 ; i++)//1-99
    {
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
    }
	
	j = j_art+150;
	
	for (i=i_art; i<=199 ; i++)//1-99
    {
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
    }


     j = j_art+200;
	
	for (i=i_art; i<=199 ; i++)//1-99
    {
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
    }


	
	
	
	
	
	i=50;
	for(j=0;j<=200;j++)
	{
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
	}
	
	i=151;
	for(j=0;j<=200;j++)
	{
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
	}
	
	i=200;
	for(j=0;j<=200;j++)
	{
		VesselListLength++;
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		pCurrentVesselPoint->i_origin = i;
		pCurrentVesselPoint->j_origin = j;
		
		
		pCurrentVesselPoint->MyAge = 3;
		pCurrentVesselPoint->AmIArtery = 0;
		VesselPoint* pNewVesselPoint = new VesselPoint;
		pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
		pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		
	}

	
	
	
/****************************************************/
		
	pLastVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	pLastVesselPoint -> pNextVesselPoint = NULL;
	//cout <<"function ArteryPosition finished"<<endl; 
	
}

void VesselStructure::ArteryPosition_R()
{   
	//cout <<"function ArteryPosition running"<<endl; 
	extern int Xnodes;
	extern int Ynodes;
	extern Matrix VesselCells;
	extern Matrix SproutTips;
	extern Matrix MapIndicator;
	extern Matrix Tracer, TracerX, TracerY, TracerS, Wss;
	extern Matrix MapVesselCells;
	extern Matrix MapSproutTips;
    extern Matrix MapFlow;
    extern Matrix MapRadius;
	extern Matrix MapPressure;
	extern Matrix VesselAge;
	extern Matrix BloodVesselIndicator, BloodVesselIndicatorX,  BloodVesselIndicatorY;
    pFirstVesselPoint = new VesselPoint;
	pFirstVesselPoint->pPreviousVesselPoint = NULL;
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	Wss = zeros(Xnodes, Ynodes);
	Tracer = zeros(Xnodes, Ynodes);
	TracerS = zeros(Xnodes, Ynodes);
	TracerX = zeros(Xnodes, Ynodes);
	TracerY = zeros(Xnodes, Ynodes);
	VesselCells = zeros(Xnodes, Ynodes);
	SproutTips = zeros(Xnodes, Ynodes);	
	MapIndicator = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapVesselCells =  zeros(2*Xnodes-1, 2*Ynodes-1);
	MapSproutTips = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapFlow = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapRadius = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapPressure = zeros(2*Xnodes-1, 2*Ynodes-1);
	VesselAge = zeros(Xnodes,Ynodes);	
    





/*This is for chessboard*/
	int head = 1;//95;//90;
	int tail = 199;//106;//110;
	int div = 11;//22;
	
	
	
	int j=head;
    int i=head; 
	
	while( j<= tail)
	{
	for (i=head; i<= tail; i++)
    {
	VesselListLength++;
	pCurrentVesselPoint->i = i;
	pCurrentVesselPoint->j = j;
	pCurrentVesselPoint->i_origin = i;
	pCurrentVesselPoint->j_origin = j;
   *VesselCells(i,j) = 6;
   *VesselAge(i, j) = 3;
	  CellNumber +=6;
	pCurrentVesselPoint->MyAge = 3;
	pCurrentVesselPoint->AmIArtery = 0;
	VesselPoint* pNewVesselPoint = new VesselPoint;
	pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
	
	
    }
	
	j=j+div;
	}
	
	
    i= head;
	j= head;
	while(i<=tail)
	{
	
	if((i-head)%div==0)
	{
	j=head;
	while(j<=tail)
	{	
		if((j-head)%div!=0)
		{
	VesselPoint* pNewVesselPoint = new VesselPoint;
	pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
		*VesselCells(i,j) = 6;
		*VesselAge(i, j) = 3;
	  CellNumber +=6;
	  pCurrentVesselPoint->MyAge = 3;
		VesselListLength++;
	pCurrentVesselPoint->i = i;
	pCurrentVesselPoint->j = j;
	pCurrentVesselPoint->i_origin = i;
	pCurrentVesselPoint->j_origin = j;
	}
	
	j++;
	
	}
	
	
	}
	i++;
	}



pLastVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;

/******************This is for chessboard*/




	/*
	*VesselCells(head,j) = 2;
	CellNumber +=2;
    VesselListLength++;
	pCurrentVesselPoint->i = head;
	pCurrentVesselPoint->j = j;
	pCurrentVesselPoint->i_origin = head;
	pCurrentVesselPoint->j_origin = j;
	//pCurrentVesselPoint->MyAge = 0;
	pCurrentVesselPoint->AmIArtery = 0;
	VesselPoint* pNewVesselPoint = new VesselPoint;
	pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
	
	
	*VesselCells(tail-1,j) = 2;
	CellNumber +=2;
	VesselListLength++;
	pCurrentVesselPoint->i = tail-1;
	pCurrentVesselPoint->j = j;
	pCurrentVesselPoint->i_origin = tail-1;
	pCurrentVesselPoint->j_origin = j;
	//pCurrentVesselPoint->MyAge = 0;
	pCurrentVesselPoint->AmIArtery = 0;
	pNewVesselPoint = new VesselPoint;
	pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward

	

		
	pLastVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;
	
	*/
	
	
	
	//pLastVesselPoint -> pNextVesselPoint = NULL;
//cout <<"function ArteryPosition finished"<<endl; 

}

	


bool VesselStructure::CheckTAF( double Tolerance)
{
//cout <<"function CheckTAF running"<<endl; 
extern Matrix TAF;
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
	int i = pCurrentVesselPoint->i;
	int j = pCurrentVesselPoint->j;
	if(*TAF(i, j) >= Tolerance)
		{
			return true;
		}
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}
//cout <<"function CheckTAF finished"<<endl; 
return 0;

}


void VesselStructure::InitializeVesselListInOrder(bool VesselResume)
{	
	//cout <<"function InitializeVesselListInOrder running"<<endl; 
	extern int Xnodes, Ynodes;
	extern bool AngiogenesisIsStarted;
	//extern Matrix BloodVesselIndicator;
	
    
	if (VesselResume == 0 && !AngiogenesisIsStarted)
	{
	
	//int InitialSprout = 5;
	SproutNumber = 0; 
	
		VesselPoint* pCurrentVesselPoint = pLastVesselPoint;
		
		
		int i=0;
		int j=0;
		
	while(i<=Xnodes-1)
	{
	
	if(i!=(Xnodes-1)/2)
	{
	j=0;
	while(j<=Ynodes-1)
	{	
		if(j%25!=0||j==(Ynodes-1)/2)//modified for special purpose
		{
	VesselPoint* pNewVesselPoint = new VesselPoint;
	pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
	
	
	//put the artery at the top
	//in out matrices, i goes from left to right as 0->1 
	//                 j goes form bottome to top as 0->1
	
	//put the sprout
	//for (int i=0; i< InitialSprout; i++)
    //{
	VesselListLength++;
	//SproutNumber++;
    //int Position = (int) floor( 1.0*(i + 0.5)*(Xnodes-1)/(InitialSprout) );//Min's Notes 1 :We should change here related to TAF
	
	pCurrentVesselPoint->i = i;
	pCurrentVesselPoint->j = j;
	}
	
	j++;
	
	}
	}
	i=i+25;
	}
   
   
  	
	
	pLastVesselPoint = pCurrentVesselPoint;
	//pLastVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;	
	//pLastVesselPoint -> pNextVesselPoint = NULL;
    }
	else
	{
	
 extern char SaveDirectory [1025];
 char temp_string [1024];
 sprintf( temp_string , "%s/pVesselList_resume.dat" , SaveDirectory );
 ifstream pVesselList_resume( temp_string, ios::in ); 
	pFirstVesselPoint = new VesselPoint;
	pFirstVesselPoint->pPreviousVesselPoint = NULL;
    VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	pVesselList_resume >> VesselListLength;
	pVesselList_resume >> SproutNumber;
	
	
		
	 

		

	
 for(int i=0;i<VesselListLength; i++)
 {
    		
	pVesselList_resume >> pCurrentVesselPoint->i;
	pVesselList_resume >> pCurrentVesselPoint->j;
	pVesselList_resume >> pCurrentVesselPoint->AmIArtery ;
	pVesselList_resume >> pCurrentVesselPoint->AmIVenous ;

 
	VesselPoint* pNewVesselPoint = new VesselPoint;
	pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
	
}
 pCurrentVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;
	delete pCurrentVesselPoint->pNextVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint=NULL;
	pLastVesselPoint = pCurrentVesselPoint;

	
	
	/**
	 extern char SaveDirectory [1025];
 char temp_string [1024];
 sprintf( temp_string , "%s/pVesselList_resume.dat" , SaveDirectory );
 ifstream pVesselList_resume( temp_string, ios::in ); 
	pFirstVesselPoint = new VesselPoint;
	pFirstVesselPoint->pPreviousVesselPoint = NULL;
    VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	pVesselList_resume >> VesselListLength;
	pVesselList_resume >> SproutNumber;
	
	VesselListLength=0;
	
	for(int i=0;i<Xnodes;i++)
	{
		for(int j=0;j<Ynodes;j++)
		{
		
		if(*BloodVesselIndicator(i,j)>0)
		{			
	
		pCurrentVesselPoint->i = i;
		pCurrentVesselPoint->j = j;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint = pNewVesselPoint;	
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//never forget to move forward
	VesselListLength++;
			
		}
		}
	
	}
	
	VesselListLength--;
	pCurrentVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;
	delete pCurrentVesselPoint->pNextVesselPoint;
	pCurrentVesselPoint->pNextVesselPoint=NULL;
	pLastVesselPoint = pCurrentVesselPoint;
	**/
	
	}
	//cout <<"function InitializeVesselListInOrder finished"<<endl; 

	
}

// Update SproutTips, BloodVesselIndicator, DirectionX, DirectionY
void VesselStructure::InitializeVesselMatrix(bool VesselResume)
{
	
	cout <<"function InitializeVesselMatrix running"<<endl; 
	double Rmin, Rmax, mu_blood;
	double pi = 3.141592654;
	int div=25;
	Rmin = RetrieveDoubleParameter( ParameterFile , "Rmin");
	Rmax = RetrieveDoubleParameter( ParameterFile , "Rmax");
	mu_blood = RetrieveDoubleParameter( ParameterFile , "mu_blood");
	
	MaxWss = RetrieveDoubleParameter( ParameterFile , "MaxWss");
	if( MaxWss < 1e-5 )
	{ MaxWss = 2.0; }
	double ArteryRadius;
	ArteryRadius = RetrieveDoubleParameter( ParameterFile , "ArteryRadius");
	
	
	extern bool AngiogenesisIsStarted;
	extern int Xnodes, Ynodes;
	
	extern Matrix BloodVesselIndicator;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix BloodVesselRadius;
	extern Matrix Stability;
	//mextern Matrix BloodVesselFlowRate;
	extern Matrix HaematocritLevel;
	extern Matrix SproutTips;
	extern Matrix SproutAge;
	extern Matrix VesselAge;
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodNodePressure;
	extern Matrix BloodVesselPressureX;
	extern Matrix BloodVesselPressureY;
	//extern Matrix DirectionX, DirectionY;
	extern Matrix FlowX, FlowY;
	extern Matrix WssX, WssY;
	
	extern Matrix VisX, VisY; //viscosity
	extern Matrix ConductX, ConductY; //conductivity
	double RandNumber;
	
	if (VesselResume == 0 && !AngiogenesisIsStarted)
	{
		
		BloodVesselIndicator = zeros( Xnodes , Ynodes );
		BloodVesselIndicatorX = zeros( Xnodes , Ynodes );
		BloodVesselIndicatorY = zeros( Xnodes , Ynodes );
		HaematocritLevel = zeros( Xnodes , Ynodes );
		SproutTips = zeros( Xnodes , Ynodes );
		SproutAge = zeros(Xnodes, Ynodes);
		VesselAge = zeros(Xnodes, Ynodes);
		BloodVesselRadiusXdir = zeros(Xnodes , Ynodes);
		BloodVesselRadiusYdir = zeros(Xnodes , Ynodes);
		for(int i = 0; i< Xnodes; i++)
		{
			for(int j = 0; j< Ynodes; j++)
			{
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) = Rmin + RandNumber*(Rmax - Rmin);
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusYdir(i, j) = Rmin + RandNumber*(Rmax - Rmin);
			}
			
		}
		BloodVesselRadius = zeros( Xnodes , Ynodes );
		BloodNodePressure = zeros( Xnodes, Ynodes );
		BloodVesselPressureX = zeros( Xnodes, Ynodes );
		BloodVesselPressureY = zeros( Xnodes, Ynodes );
		Stability = zeros(Xnodes, Ynodes);
		//Direction Matrix only matters to Sprouts
		//DirectionX = zeros( Xnodes, Ynodes ); // 0 means no vessel, 1, -1 means one way direction
		//DirectionY = zeros( Xnodes, Ynodes );
		
		FlowX = zeros( Xnodes, Ynodes );
		FlowY = zeros( Xnodes, Ynodes );
		WssX = zeros( Xnodes, Ynodes );
		WssY = zeros( Xnodes, Ynodes );
		
		VisX = ones( Xnodes, Ynodes );
		VisY = ones( Xnodes, Ynodes );
		ConductX = zeros( Xnodes, Ynodes );
		ConductY = zeros( Xnodes, Ynodes );
		VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
		
	

	int i=0;
	int j=0;
	
	/*************************1*************************************/		
   	
		int i_art = 0;//1-99
		int j_art = 0;//0,100,200
		int i_ven = 101;//101-199
		int j_ven = 50;//50,150	
		
		i = i_art;
		j = j_art;
		
		*BloodVesselIndicatorX(i, j) = 1;
		*BloodVesselIndicator(i, j) = 1;
		*Stability(i,j) =1;
		*VisX(i, j) = mu_blood;
		
		*ConductX(i, j) = 0;		
		
		
		for(i=1;i<50;i++)
		{
				*Stability(i,j) =1;
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
				*VisX(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		}
		
		i = i_art;
		j = j_art+50;
		
		*BloodVesselIndicatorX(i, j) = 1;
		*BloodVesselIndicator(i, j) = 1;
		*Stability(i,j) =1;
		*VisX(i, j) = mu_blood;
		
		*ConductX(i, j) = 0;		
		
		
		for(i=1;i<50;i++)
		{
				*Stability(i,j) =1;
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
				*VisX(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		}
		
		i = i_art;
		j = j_art+150;
		
		*BloodVesselIndicatorX(i, j) = 1;
		*BloodVesselIndicator(i, j) = 1;
		*Stability(i,j) =1;
		*VisX(i, j) = mu_blood;
		
		*ConductX(i, j) = 0;		
		
		
		for(i=1;i<50;i++)
		{
				*Stability(i,j) =1;
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
				*VisX(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		}
		
		
		
		i = i_art;
		j = j_art+200;
		
		*BloodVesselIndicatorX(i, j) = 1;
		*BloodVesselIndicator(i, j) = 1;
		*Stability(i,j) =1;
		*VisX(i, j) = mu_blood;
		
		*ConductX(i, j) = 0;		
		
		
		for(i=1;i<50;i++)
		{
				*Stability(i,j) =1;
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
				*VisX(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		}

		
		i = i_art+51;
		j = j_art;
		
	
		for(i=i_art+51;i<152;i++)
		{
				*Stability(i,j) =1;
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
				*VisX(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		}
		
		i = i_art+51;
		j = j_art+50;
		
	
		for(i=i_art+51;i<152;i++)
		{
				*Stability(i,j) =1;
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
				*VisX(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		}
		
		i = i_art+51;
		j = j_art+150;
		
	
		for(i=i_art+51;i<152;i++)
		{
				*Stability(i,j) =1;
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
				*VisX(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		}
		
		i = i_art+51;
		j = j_art+200;
		
	
		for(i=i_art+51;i<152;i++)
		{
				*Stability(i,j) =1;
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
				*VisX(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		}

		
		i = i_art+152;
		j = j_art;
		
	
		
		for(i=i_art+152;i<200;i++)
		{
				*Stability(i,j) =1;
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
				*VisX(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		}
		
		
		*BloodVesselIndicatorX(i, j) = -1;
		*BloodVesselIndicator(i, j) = 1;
		*Stability(i,j) =1;
		*VisX(i, j) = mu_blood;
		
		RandNumber = float(rand())/float(RAND_MAX);
		*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
		*VisX(i, j) = mu_blood;
				
				
		*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
	
		
        i = i_art+152;
		j = j_art+50;
		
	
		
		for(i=i_art+152;i<200;i++)
		{
				*Stability(i,j) =1;
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
				*VisX(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		}
		
		
		*BloodVesselIndicatorX(i, j) = -1;
		*BloodVesselIndicator(i, j) = 1;
		*Stability(i,j) =1;
		*VisX(i, j) = mu_blood;
		
		RandNumber = float(rand())/float(RAND_MAX);
		*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
		*VisX(i, j) = mu_blood;
				
				
		*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
		
		 i = i_art+152;
		j = j_art+150;
		
	
		
		for(i=i_art+152;i<200;i++)
		{
				*Stability(i,j) =1;
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
				*VisX(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		}
		
		
		*BloodVesselIndicatorX(i, j) = -1;
		*BloodVesselIndicator(i, j) = 1;
		*Stability(i,j) =1;
		*VisX(i, j) = mu_blood;
		
		RandNumber = float(rand())/float(RAND_MAX);
		*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
		*VisX(i, j) = mu_blood;
				
				
		*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);


        i = i_art+152;
		j = j_art+200;
		
	
		
		for(i=i_art+152;i<200;i++)
		{
				*Stability(i,j) =1;
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				RandNumber = float(rand())/float(RAND_MAX);
				*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
				*VisX(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		}
		
		
		*BloodVesselIndicatorX(i, j) = -1;
		*BloodVesselIndicator(i, j) = 1;
		*Stability(i,j) =1;
		*VisX(i, j) = mu_blood;
		
		RandNumber = float(rand())/float(RAND_MAX);
		*BloodVesselRadiusXdir(i, j) =  Rmin + RandNumber*(Rmax - Rmin);
		*VisX(i, j) = mu_blood;
				
				
		*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				
		/****************************middle line******************/
		
		i=50;
		j=0;
		
		*BloodVesselIndicatorX(i, j) = 2;
		*BloodVesselIndicatorY(i, j) = 1;
		*BloodVesselIndicator(i, j) = 3;
		
		
		        *VisX(i, j) = mu_blood;
				*VisY(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);
	
		
		
				
		 for(j=1;j<200;j++)
		{
						
			 if(j==50)
			{
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicatorY(i, j) = 2;
				*BloodVesselIndicator(i, j) = 4;
				
				*VisX(i, j) = mu_blood;
				*VisY(i, j) = mu_blood;
				
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);
			}
			else if(j==150)
			{
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicatorY(i, j) = 2;
				*BloodVesselIndicator(i, j) = 4;
				
				
				*VisX(i, j) = mu_blood;
				*VisY(i, j) = mu_blood;
				
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);
			}
			else 
			{
				*BloodVesselIndicatorY(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				
				
				*VisY(i, j) = mu_blood;				
				
				
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);	
			}
			
			
						
		}
		
		
		
		*BloodVesselIndicatorX(i, j) = 2;
		*BloodVesselIndicatorY(i, j) = -1;
		*BloodVesselIndicator(i, j) = 3;
		
		*VisX(i, j) = mu_blood;
		*VisY(i, j) = mu_blood;
		
		*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
		*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);		
		
		
		
		i=151;
		j=0;
		
		*BloodVesselIndicatorX(i, j) = 2;
		*BloodVesselIndicatorY(i, j) = 1;
		*BloodVesselIndicator(i, j) = 3;
		
		
		        *VisX(i, j) = mu_blood;
				*VisY(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);
	
		
		
				
		 for(j=1;j<200;j++)
		{
						
			 if(j==50)
			{
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicatorY(i, j) = 2;
				*BloodVesselIndicator(i, j) = 4;
				
				*VisX(i, j) = mu_blood;
				*VisY(i, j) = mu_blood;
				
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);
			}
			else if(j==150)
			{
				*BloodVesselIndicatorX(i, j) = 2;
				*BloodVesselIndicatorY(i, j) = 2;
				*BloodVesselIndicator(i, j) = 4;
				
				
				*VisX(i, j) = mu_blood;
				*VisY(i, j) = mu_blood;
				
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);
			}
			else 
			{
				*BloodVesselIndicatorY(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				
				
				*VisY(i, j) = mu_blood;				
				
				
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);	
			}
			
			
						
		}
		
		
		
		*BloodVesselIndicatorX(i, j) = 2;
		*BloodVesselIndicatorY(i, j) = -1;
		*BloodVesselIndicator(i, j) = 3;
		
		*VisX(i, j) = mu_blood;
		*VisY(i, j) = mu_blood;
		
		*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
		*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);		
		
		
		
		
		i=200;
		j=0;
		
		*BloodVesselIndicatorX(i, j) = -1;
		*BloodVesselIndicatorY(i, j) = 1;
		*BloodVesselIndicator(i, j) = 2;
		
		
		        *VisX(i, j) = mu_blood;
				*VisY(i, j) = mu_blood;
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);
	
		
		
				
		 for(j=1;j<200;j++)
		{
						
			 if(j==50)
			{
				*BloodVesselIndicatorX(i, j) = -1;
				*BloodVesselIndicatorY(i, j) = 2;
				*BloodVesselIndicator(i, j) = 3;
				
				*VisX(i, j) = mu_blood;
				*VisY(i, j) = mu_blood;
				
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);
			}
			else if(j==150)
			{
				*BloodVesselIndicatorX(i, j) = -1;
				*BloodVesselIndicatorY(i, j) = 2;
				*BloodVesselIndicator(i, j) = 3;
				
				
				*VisX(i, j) = mu_blood;
				*VisY(i, j) = mu_blood;
				
				
				
				*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);
			}
			else 
			{
				*BloodVesselIndicatorY(i, j) = 2;
				*BloodVesselIndicator(i, j) = 2;
				
				
				
				*VisY(i, j) = mu_blood;				
				
				
				*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);	
			}
			
			
						
		}
		
		
		
		*BloodVesselIndicatorX(i, j) = -1;
		*BloodVesselIndicatorY(i, j) = -1;
		*BloodVesselIndicator(i, j) = 2;
		
		*VisX(i, j) = mu_blood;
		*VisY(i, j) = mu_blood;
		
		*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j) , 4)/(8.0* *VisX(i, j)*NodeLength);
		*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j) , 4)/(8.0* *VisY(i, j)*NodeLength);		
		

		
		
		
		
	}
	else if (VesselResume == 1 && !AngiogenesisIsStarted)
		
	{
		
		FlowX = zeros( Xnodes, Ynodes );
		FlowY = zeros( Xnodes, Ynodes );
		WssX = zeros( Xnodes, Ynodes );
		WssY = zeros( Xnodes, Ynodes );
		
		VisX = ones( Xnodes, Ynodes );
		VisY = ones( Xnodes, Ynodes );
		ConductX=zeros(Xnodes,Ynodes);
		ConductY=zeros(Xnodes,Ynodes);
		//for (int i=0; i< Xnodes; i++)
		//{
		//	for(int j=0; j<Ynodes; j++)
		//	{
		//	*ConductX(i, j) = 1e-25;
		//	*ConductY(i, j) = 1e-25;
		//cout<<*ConductX(i, j);
		//	}
		//}
		BloodNodePressure = zeros( Xnodes, Ynodes );
		UpdateViscosity();
		UpdateConductivity();
		//add things here
	}
	
	
	
	
	
	cout <<"function InitializeVesselMatrix finished"<<endl; 
}

void VesselStructure::InitializeVesselMatrix_R(bool VesselResume)
{

cout <<"function InitializeVesselMatrix running"<<endl; 
 double Rmin, Rmax, mu_blood;
 double pi = 3.141592654;

 Rmin = RetrieveDoubleParameter( ParameterFile , "Rmin");
 Rmax = RetrieveDoubleParameter( ParameterFile , "Rmax");
 mu_blood = RetrieveDoubleParameter( ParameterFile , "mu_blood");
 
 MaxWss = RetrieveDoubleParameter( ParameterFile , "MaxWss");
	if( MaxWss < 1e-5 )
	{ MaxWss = 2.0; }
 double ArteryRadius;
	ArteryRadius = RetrieveDoubleParameter( ParameterFile , "ArteryRadius");


 extern bool AngiogenesisIsStarted;
 extern int Xnodes, Ynodes;
 
 extern Matrix BloodVesselIndicator;
 extern Matrix BloodVesselIndicatorX;
 extern Matrix BloodVesselIndicatorY;
 extern Matrix BloodVesselRadius;
 //mextern Matrix BloodVesselFlowRate;
 extern Matrix HaematocritLevel;
 extern Matrix SproutTips;
 extern Matrix SproutAge;
 extern Matrix VesselAge;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodNodePressure;
 extern Matrix BloodVesselPressureX;
  extern Matrix BloodVesselPressureY;
 //extern Matrix DirectionX, DirectionY;
 extern Matrix FlowX, FlowY;
 extern Matrix WssX, WssY;
 
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 double RandNumber;

 if (VesselResume == 0 && !AngiogenesisIsStarted)
 {
 
 BloodVesselIndicator = zeros( Xnodes , Ynodes );
 BloodVesselIndicatorX = zeros( Xnodes , Ynodes );
 BloodVesselIndicatorY = zeros( Xnodes , Ynodes );
 HaematocritLevel = zeros( Xnodes , Ynodes );
 SproutTips = zeros( Xnodes , Ynodes );
 SproutAge = zeros(Xnodes, Ynodes);
 //VesselAge = zeros(Xnodes, Ynodes);
 BloodVesselRadiusXdir = zeros(Xnodes , Ynodes);
 BloodVesselRadiusYdir = zeros(Xnodes , Ynodes);
 for(int i = 0; i< Xnodes; i++)
 {
	for(int j = 0; j< Ynodes; j++)
	{
		RandNumber = float(rand())/float(RAND_MAX);
		*BloodVesselRadiusXdir(i, j) = Rmin + RandNumber*(Rmax - Rmin);
		RandNumber = float(rand())/float(RAND_MAX);
		*BloodVesselRadiusYdir(i, j) = Rmin + RandNumber*(Rmax - Rmin);
	}
 
 }
 
 BloodVesselRadius = zeros( Xnodes , Ynodes );
 BloodNodePressure = zeros( Xnodes, Ynodes );
 BloodVesselPressureX = zeros( Xnodes, Ynodes );
  BloodVesselPressureY = zeros( Xnodes, Ynodes );
//Direction Matrix only matters to Sprouts
    //DirectionX = zeros( Xnodes, Ynodes ); // 0 means no vessel, 1, -1 means one way direction
	//DirectionY = zeros( Xnodes, Ynodes );
	
	FlowX = zeros( Xnodes, Ynodes );
	FlowY = zeros( Xnodes, Ynodes );
	WssX = zeros( Xnodes, Ynodes );
	WssY = zeros( Xnodes, Ynodes );
	
	VisX = ones( Xnodes, Ynodes );
	VisY = ones( Xnodes, Ynodes );
	ConductX = zeros( Xnodes, Ynodes );
	ConductY = zeros( Xnodes, Ynodes );
	int head = 1;//95;//90;
	int tail = 199;//106;//110;
	int div = 11;//22;//66;//2
	int i = head;
	int j = head;
	   //indicator matrix below
	
	for (i=head; i<= tail; i++)
    {
		
		
		//cout<<pCurrentVesselPoint->i<<"    "<<pCurrentVesselPoint->j<<endl;
		
		if((i-head)%div==0)
		{
		*BloodVesselIndicatorX(i, j) = 2;
		*BloodVesselIndicatorY(i, j) = 1;
		
		*BloodVesselIndicator(i, j) = 3;
		*BloodVesselRadiusXdir(i, j) = ArteryRadius;
		
		
		if(i==head)
		{
		
		*BloodVesselIndicatorX(i, j) = 1;
		*BloodVesselIndicatorY(i, j) = 1;
		*BloodVesselIndicator(i, j) = 2;
		//*BloodVesselRadiusXdir(i, j) = 0;
		//*BloodVesselRadiusYdir(i, j) = 0;
		}
		else if(i==tail)
		{
		
		*BloodVesselIndicatorX(i, j) = -1;
		*BloodVesselIndicatorY(i, j) = 1;
		*BloodVesselIndicator(i, j) = 2;
		*BloodVesselRadiusXdir(i, j) = ArteryRadius;
		//*BloodVesselRadiusYdir(i, j) = 0;

		}

		


		}
		else
		{
				
		*BloodVesselIndicatorX(i, j) = 2;
		*BloodVesselIndicator(i, j) = 2;
		*BloodVesselRadiusXdir(i, j) = ArteryRadius;
		//*BloodVesselRadiusYdir(i, j) = 0;
		


		}

				//cout<<"Con"<<*ConductX(i,j)<<endl;
	 	}
	
	j=head+1;

	while(j<tail)
	{
	
	if((j-head)%div==0)
	{
	 i=head;
		*BloodVesselIndicatorX(i, j) = 1;
	   

		*BloodVesselIndicatorY(i, j) = 2;	
		
	    *BloodVesselIndicator(i, j) = 3;
		//*BloodVesselRadiusXdir(i, j) = 0;
		*BloodVesselRadiusYdir(i, j) = ArteryRadius;

	
	for( i=head+1;i<tail; i++)
	{
	
	
	if((i-head)%div==0)
	{
		*BloodVesselIndicatorX(i, j) = 2;
	   

		*BloodVesselIndicatorY(i, j) = 2;	
	
		
	    *BloodVesselIndicator(i, j) = 4;
		*BloodVesselRadiusXdir(i, j) = ArteryRadius;
		*BloodVesselRadiusYdir(i, j) = ArteryRadius;
	}
	else
	{
	
		*BloodVesselIndicatorX(i, j) = 2;
	   			
		*BloodVesselIndicator(i, j) = 2;
		*BloodVesselRadiusXdir(i, j) = ArteryRadius;
		//*BloodVesselRadiusYdir(i, j) = 0;
				
	}
				
		//*DirectionX(i, j) = 1;
	
	
	}
	i=tail;
	
	
		*BloodVesselIndicatorX(i, j) = -1;
	   
		
		
		*BloodVesselIndicatorY(i, j) = 2;	
	
		
	*BloodVesselIndicator(i, j) = 3;
	
	*BloodVesselRadiusXdir(i, j) = ArteryRadius;
	*BloodVesselRadiusYdir(i, j) = ArteryRadius;

	
	
		
	}
	else
	{
	   for(i=head;i<tail; i=i+div)
		{
		
		*BloodVesselIndicatorY(i, j) = 2;	
		*BloodVesselIndicator(i, j) = 2;
		//*BloodVesselRadiusXdir(i, j) = 0;
		*BloodVesselRadiusYdir(i, j) = ArteryRadius;
		//*DirectionX(i, j) = 1;
		
		}
		
		i=tail;
	
	
		
	   
		
		
		*BloodVesselIndicatorY(i, j) = 2;	
	
		
	*BloodVesselIndicator(i, j) = 2;	
	
	//*BloodVesselRadiusXdir(i, j) = 0;
		*BloodVesselRadiusYdir(i, j) = ArteryRadius;
	}
	
	
	
	j++;
	}
	
	
	
	j=tail;
	
	for (i=head; i<= tail; i++)
    {
		if((i-head)%div==0)
		{
		
		*BloodVesselIndicatorX(i, j) = 2;
		*BloodVesselIndicatorY(i, j) = -1;
		*BloodVesselIndicator(i, j) = 3;
		
		*BloodVesselRadiusXdir(i, j) = ArteryRadius;
		*BloodVesselRadiusYdir(i, j) = ArteryRadius;
		
		if(i==head)
		{
		
		*BloodVesselIndicatorX(i, j) = 1;
		*BloodVesselIndicatorY(i, j) = -1;
		*BloodVesselIndicator(i, j) = 2;
		//*BloodVesselRadiusXdir(i, j) = 0;
		
		*BloodVesselRadiusYdir(i, j) = ArteryRadius;

		}
		else if(i==tail)
		{
		
		*BloodVesselIndicatorX(i, j) = -1;
		*BloodVesselIndicatorY(i, j) = -1;
		*BloodVesselIndicator(i, j) = 2;
		
		*BloodVesselRadiusXdir(i, j) = ArteryRadius;
		*BloodVesselRadiusYdir(i, j) = ArteryRadius;

		}

		}
		else
		{
		*BloodVesselIndicatorX(i, j) = 2;
		*BloodVesselIndicator(i, j) = 2;
		
		*BloodVesselRadiusXdir(i, j) = ArteryRadius;
		}
		
	}

	
			
	BloodNodePressure = zeros( Xnodes, Ynodes );
	UpdateViscosity();
	UpdateConductivity();			

	
	}
	else if (VesselResume == 1 && !AngiogenesisIsStarted)

	{
	
	FlowX = zeros( Xnodes, Ynodes );
	FlowY = zeros( Xnodes, Ynodes );
	WssX = zeros( Xnodes, Ynodes );
	WssY = zeros( Xnodes, Ynodes );
	
	VisX = ones( Xnodes, Ynodes );
	VisY = ones( Xnodes, Ynodes );
	ConductX=zeros(Xnodes,Ynodes);
	ConductY=zeros(Xnodes,Ynodes);
	//for (int i=0; i< Xnodes; i++)
	//{
	//	for(int j=0; j<Ynodes; j++)
	//	{
	//	*ConductX(i, j) = 1e-25;
	//	*ConductY(i, j) = 1e-25;
		//cout<<*ConductX(i, j);
	//	}
	//}
	//BloodNodePressure = zeros( Xnodes, Ynodes );
	//UpdateViscosity();
	//UpdateConductivity();
	//add things here
	}
	
	
	

  
	cout <<"function InitializeVesselMatrix_R finished"<<endl; 
}


void VesselStructure::GetRadiusValue()
{
//cout <<"function GetRadiusValue running"<<endl; 
extern Matrix BloodVesselRadiusXdir;
extern Matrix BloodVesselRadiusYdir;
extern Matrix BloodVesselRadius;
extern Matrix BloodVesselIndicator;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;

extern int Xnodes, Ynodes;
BloodVesselRadius = zeros(Xnodes, Ynodes);
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
int i= pCurrentVesselPoint->i;
int j= pCurrentVesselPoint->j;
	if(i!=(Xnodes-1)&&j!=(Ynodes-1)&&i!=0&&j!=0)//don't consider the boundary
		{
			*BloodVesselRadius(i,j) = 0;	
			
			if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
				{
					*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i,j);
					*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i+1,j);
					
				}
			else if(*BloodVesselIndicatorX(i,j)>0.5)
				{
					*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i+1,j);
				}
			else if(*BloodVesselIndicatorX(i,j)<-0.5)
				{
					*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i,j);
				}


			if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
				{
					*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j);
					*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j+1);
					
				}
			else if(*BloodVesselIndicatorY(i,j)>0.5)
				{
					*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j+1);
				}
			else if(*BloodVesselIndicatorY(i,j)<-0.5)
				{
					*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j);
				}





		*BloodVesselRadius(i,j) 
			/=(min_abs(*BloodVesselIndicatorX(i,j))+min_abs(*BloodVesselIndicatorY(i,j)));

		}
pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}

//cout <<"function GetRadiusValue finished"<<endl; 
}






bool VesselStructure::SaveVesselList()
{
//cout <<"function SaveListInOrder running"<<endl; 

extern bool AngiogenesisIsStarted;
extern bool AngiogenesisDisabled;

if( AngiogenesisIsStarted == false ||
     AngiogenesisDisabled == true )
 { 
 return false; 
 }	
 
 extern char SaveDirectory[1025];
 char temp_string[1024];
 
 sprintf( temp_string, "%s/angio_resume.dat", SaveDirectory);
 ofstream angio_resume( temp_string, ios::out );
 angio_resume << AngioDeltaT <<"\t"
			  
              		  
              << endl;	
cout<<AngioDeltaT<<" save  "<<endl;				  
 angio_resume.close();	
 
 
 //save list now
  sprintf( temp_string , "%s/pVesselList_resume.dat" , SaveDirectory );
 ofstream pVesselList_resume( temp_string, ios::out );
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 pVesselList_resume << VesselListLength <<"\t";
 pVesselList_resume << SproutNumber<<"\t";
 while(pCurrentVesselPoint)
 {
 pVesselList_resume << pCurrentVesselPoint->i<<"\t"
				    << pCurrentVesselPoint->j<<"\t"
					<< pCurrentVesselPoint->AmIArtery<<"\t"
					<< pCurrentVesselPoint->AmIVenous<<"\t";
					
 pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;					
 }
 pVesselList_resume.close();
// cout <<"function SaveListInOrder finished"<<endl;
 return true;
 }
 
 
 
void VesselStructure::ChainAngioPressureUpdate(int MaxIterations, double Tolerance)
{
	extern Matrix ConductX,ConductY;
	extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
	extern Matrix BloodNodePressure;
	extern Matrix X,Y;
	extern int Xnodes, Ynodes;
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;//initialize the updating point
	double Error=2*Tolerance;
	double MaxError=2*Tolerance;
	int Iterations=0;
	double NewValue=0.0;
	int i=0;
	int j=0;
	double Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
	double Pout = RetrieveDoubleParameter( ParameterFile , "Pout");
	double MaxConduct= 1e-25;
	for(int ii=0; ii<X.TellCols(); ii++)
	{
		for(int jj=0; jj<Y.TellCols(); jj++)
		{
			//cout<<*BloodVesselIndicatorX(ii,jj)<<endl;
			if(*ConductX(ii,jj)>MaxConduct)
			{MaxConduct=*ConductX(ii,jj);}
			if(*ConductY(ii,jj)>MaxConduct)
			{MaxConduct=*ConductY(ii,jj);}
		}
	}
	//BloodNodePressure=zeros(Xnodes,Ynodes);		
	
	while(MaxError>Tolerance&&Iterations<MaxIterations)
	{
		//relocalize the point
		pCurrentVesselPoint = pFirstVesselPoint;
		MaxError=0;
		//sweep along the chain
		while(pCurrentVesselPoint)
		{
			i=pCurrentVesselPoint->i;
			j=pCurrentVesselPoint->j;
			//if(i==0)
			//{
			//NewValue = Pin;
			//}
			
			
			if(pCurrentVesselPoint->AmIArtery)
			{
				NewValue = Pin;
			}
			else if(pCurrentVesselPoint->AmIVenous)
			{
				NewValue = Pout;
			}
			/*
			 if(i==0&&j==Ynodes-1)
			 {
			 NewValue = Pin;
			 }
			 else if(i==0&&j==0)
			 {
			 NewValue = 0.5*(Pin+Pout);
			 }
			 else if(i==Xnodes-1&&j==Ynodes-1)
			 {
			 NewValue = 0.5*(Pin+Pout);
			 }
			 else if(i==Xnodes-1&&j==0)
			 {
			 NewValue = Pout;
			 }
			 
			 */
			/*
			 else if(j==Ynodes-1)
			 {
			 NewValue = ((200-i)*Pin+(i)*(Pin+Pout)*0.5)/200;
			 }
			 else if(j==0)
			 {
			 NewValue = ((200-i)*(Pin+Pout)*0.5+(i)*Pout)/200;
			 }
			 else if(i==Xnodes-1)
			 {
			 NewValue = ((200-j)*Pout+(j)*(Pin+Pout)*0.5)/200;
			 }
			 else if(i==0)
			 {
			 NewValue = ((200-j)*(Pin+Pout)*0.5+(j)*Pin)/200;
			 }
			 
			 */
			
			else
			{
				NewValue = AngioSORUpdate( BloodNodePressure, ConductX, ConductY, BloodVesselIndicatorX, BloodVesselIndicatorY,i, j, MaxConduct);
				// if((min_abs(NewValue)>Pin))
				// {cout<<*BloodVesselIndicatorX(i,j)<<"   "<<*BloodVesselIndicatorY(i,j)<<"   "<<i<<"  ,  "<<j<<"    "<<NewValue<<"   ";}
				//cout<<"NewValue:  "<<NewValue<<endl;
			}	 
			Error = min_abs( NewValue - *BloodNodePressure(i,j) );
			if( Error > MaxError )
			{ MaxError = Error; }
			
			*BloodNodePressure(i,j) = NewValue;
			
			pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//Move on
		}
		Iterations++;
		if( Iterations % 1000 == 0 )
		{ cout << Iterations << " : " << MaxError << endl; }  
	}
	cout << "summary:: iterations: " << Iterations << endl
	<< "          error     : " << MaxError << endl << endl; 
	
	
}





/********
void SolveNodePressure()
{
//cout <<"function SolveNodePressure running"<<endl; 

double Pin, Pout;
Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
Pout = RetrieveDoubleParameter( ParameterFile , "Pout");

extern Matrix ConductX;
extern Matrix ConductY;
extern int Xnodes, Ynodes;
extern Matrix BloodNodePressure;

TwoDSOR(Xnodes, Ynodes, ConductX, ConductY, Pin, Pout);
//cout <<"function SolveNodePressure finished"<<endl; 

}

*********/
 

/******************WRONG...
void VesselStructure::GetVesselPressureAndFlow(void)
{
//cout <<"function GetVesselPressure running"<<endl; 
// extern Matrix DirectionX, DirectionY;
 extern Matrix BloodVesselIndicatorX;
 extern Matrix BloodVesselIndicatorY;
 extern Matrix BloodVesselPressureX;
 extern Matrix BloodNodePressure;
 extern Matrix FlowX, FlowY;
 extern Matrix ConductX, ConductY;
 extern int Xnodes, Ynodes;

	 double Pin, Pout;
Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
Pout = RetrieveDoubleParameter( ParameterFile , "Pout");


 BloodVesselPressureX = zeros(Xnodes, Ynodes);
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
int i = pCurrentVesselPoint->i;
int j = pCurrentVesselPoint->j;

//X direction
if(i == 0)//boundary
{
	

 if(*BloodVesselIndicatorX(i,j)>0.5)
{
*FlowX(i, j) = ( *BloodNodePressure(i, j) - *BloodNodePressure(i+1, j) ) * *ConductX(i, j);
*BloodVesselPressureX(i,j) += ( *BloodNodePressure(i,j) + *BloodNodePressure(i+1, j))/2;
}

}

else if(i == Xnodes - 1 )//boundary
{
	
	 if(*BloodVesselIndicatorX(i,j)<-0.5)
{
*FlowX(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j) ) * *ConductX(i, j);
*BloodVesselPressureX(i,j) += ( *BloodNodePressure(i,j) + *BloodNodePressure(i-1, j))/2;
}
}

else
{
	
if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)//when the point is fully connected
{
*FlowX(i, j) = ( *BloodNodePressure(i-1, j) - *BloodNodePressure(i+1, j) ) * *ConductX(i, j)/2;
*BloodVesselPressureX(i,j) += ( 2* *BloodNodePressure(i,j) + *BloodNodePressure(i-1, j)+*BloodNodePressure(i+1,j))/2;
}
	
else if(*BloodVesselIndicatorX(i,j)<-0.5)
{
*FlowX(i, j) = ( *BloodNodePressure(i-1, j) - *BloodNodePressure(i, j) ) * *ConductX(i, j);
*BloodVesselPressureX(i,j) += ( *BloodNodePressure(i,j) + *BloodNodePressure(i-1, j))/2;
}

else if(*BloodVesselIndicatorX(i,j)>0.5)
{
*FlowX(i, j) = ( *BloodNodePressure(i, j) - *BloodNodePressure(i+1, j) ) * *ConductX(i, j);
*BloodVesselPressureX(i,j) += ( *BloodNodePressure(i,j) + *BloodNodePressure(i+1, j))/2;
}

}

// Y direction

if(j == 0)//boundary
{
	

 if(*BloodVesselIndicatorY(i,j)>0.5)
{
*FlowY(i, j) = ( *BloodNodePressure(i, j) - *BloodNodePressure(i+1, j) ) * *ConductY(i, j);
*BloodVesselPressureX(i,j) += ( *BloodNodePressure(i,j) + *BloodNodePressure(i+1, j))/2;
}

}

else if(j == Ynodes - 1 )//boundary
{
	
	 if(*BloodVesselIndicatorY(i,j)<-0.5)
{
*FlowY(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j) ) * *ConductY(i, j);
*BloodVesselPressureX(i,j) += ( *BloodNodePressure(i,j) + *BloodNodePressure(i-1, j))/2;
}

}

else
{
	
if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)//when the point is fully connected
{
*FlowY(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j+1) ) * *ConductY(i, j)/2;
*BloodVesselPressureX(i,j) += ( 2* *BloodNodePressure(i,j) + *BloodNodePressure(i, j-1)+*BloodNodePressure(i,j+1))/2;
}
	
else if(*BloodVesselIndicatorY(i,j)<-0.5)
{
*FlowY(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j) ) * *ConductY(i, j);
*BloodVesselPressureX(i,j) += ( *BloodNodePressure(i,j) + *BloodNodePressure(i, j-1))/2;
}

else if(*BloodVesselIndicatorY(i,j)>0.5)
{
*FlowY(i, j) = ( *BloodNodePressure(i, j) - *BloodNodePressure(i, j+1) ) * *ConductY(i, j);
*BloodVesselPressureX(i,j) += ( *BloodNodePressure(i,j) + *BloodNodePressure(i, j+1))/2;
}

}

*BloodVesselPressureX(i,j) = *BloodVesselPressureX(i,j) * 1.0/ (*BloodVesselIndicatorX(pCurrentVesselPoint->i,pCurrentVesselPoint->j)+*BloodVesselIndicatorY(pCurrentVesselPoint->i,pCurrentVesselPoint->j));
//cout<<*BloodNodePressure(i, j)<<endl;



pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;

}





//cout <<"function GetVesselPressure finished"<<endl; 

}

******************/


void VesselStructure::GetVesselPressureAndFlow(double Tolerance)
{
//cout <<"function GetVesselPressure running"<<endl; 
// extern Matrix DirectionX, DirectionY;
 extern Matrix BloodVesselIndicatorX;
 extern Matrix BloodVesselIndicatorY;
 extern Matrix BloodVesselPressureX;
 extern Matrix BloodVesselPressureY;
 extern Matrix BloodNodePressure;
 extern Matrix InterPressure;
 extern Matrix WallGradient;
 extern Matrix FlowX, FlowY;
 extern Matrix ConductX, ConductY;
 extern int Xnodes, Ynodes;
 //double MinConduct=1000;
double Pin, Pout;
Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
Pout = RetrieveDoubleParameter( ParameterFile , "Pout");


 BloodVesselPressureX = zeros(Xnodes, Ynodes);
 BloodVesselPressureY = zeros(Xnodes, Ynodes);
 
 
 

 
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
int i = pCurrentVesselPoint->i;
int j = pCurrentVesselPoint->j;
*WallGradient(i,j) = *BloodNodePressure(i,j)-*InterPressure(i,j);
//X direction
if(i == 0)//boundary
{
	

 if(*BloodVesselIndicatorX(i,j)>0.5)
{
*FlowX(i, j) = 0;//( *BloodNodePressure(i, j) - *BloodNodePressure(i-1, j) ) * *ConductX(i, j);

}

}

else if(i == Xnodes - 1 )//boundary
{
	
	 if(*BloodVesselIndicatorX(i,j)<-0.5)
{
*FlowX(i, j) = ( *BloodNodePressure(i-1, j) - *BloodNodePressure(i, j) ) * *ConductX(i, j);
*BloodVesselPressureX(i,j)=(*BloodNodePressure(i-1,j)+*BloodNodePressure(i,j))/2;
}
}

else
{
	
if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)//when the point is fully connected
{
*FlowX(i, j) = ( *BloodNodePressure(i-1, j) - *BloodNodePressure(i, j) ) * *ConductX(i, j);
*BloodVesselPressureX(i,j)=(*BloodNodePressure(i-1,j)+*BloodNodePressure(i,j))/2;
}
	
else if(*BloodVesselIndicatorX(i,j)<-0.5)
{
*FlowX(i, j) = ( *BloodNodePressure(i-1, j) - *BloodNodePressure(i, j) ) * *ConductX(i, j);
*BloodVesselPressureX(i,j)=(*BloodNodePressure(i-1,j)+*BloodNodePressure(i,j))/2;
}

else if(*BloodVesselIndicatorX(i,j)>0.5)
{
*FlowX(i, j) = 0.0;//( *BloodNodePressure(i, j) - *BloodNodePressure(i+1, j) ) * *ConductX(i, j);

}


}

// Y direction

if(j == 0)//boundary
{
	

 if(*BloodVesselIndicatorY(i,j)>0.5)
{
*FlowY(i, j) = 0;//( *BloodNodePressure(i, j) - *BloodNodePressure(i+1, j) ) * *ConductY(i, j);

}

}

else if(j == Ynodes - 1 )//boundary
{
	
	 if(*BloodVesselIndicatorY(i,j)<-0.5)
{
*FlowY(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j) ) * *ConductY(i, j);
*BloodVesselPressureY(i,j)=(*BloodNodePressure(i,j-1)+*BloodNodePressure(i,j))/2;
}

}

else
{
	
if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)//when the point is fully connected
{
*FlowY(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j) ) * *ConductY(i, j);
*BloodVesselPressureY(i,j)=(*BloodNodePressure(i,j-1)+*BloodNodePressure(i,j))/2;
}
	
else if(*BloodVesselIndicatorY(i,j)<-0.5)
{
*FlowY(i, j) = ( *BloodNodePressure(i, j-1) - *BloodNodePressure(i, j) ) * *ConductY(i, j);
*BloodVesselPressureY(i,j)=(*BloodNodePressure(i,j-1)+*BloodNodePressure(i,j))/2;
}

else if(*BloodVesselIndicatorY(i,j)>0.5)
{
*FlowY(i, j) = 0;//( *BloodNodePressure(i, j) - *BloodNodePressure(i, j+1) ) * *ConductY(i, j);
}

}


if(min_abs(*FlowX(i,j)/ *ConductX(i,j))<Tolerance)
{
*FlowX(i,j) = 0;
}
if(min_abs(*FlowY(i,j)/ *ConductY(i,j))<Tolerance)
{
*FlowY(i,j) = 0;

}

//cout<<*BloodNodePressure(i, j)<<endl;



pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;

}






//cout <<"function GetVesselPressure finished"<<endl; 

}


double AngioSORUpdate( Matrix& Solution, Matrix& ConductX, Matrix& ConductY, Matrix& BloodVesselIndicatorX, Matrix& BloodVesselIndicatorY, int i, int j, double MaxConduct)
{
//cout<<"AngioImplicitUpdate"<<endl;
 double dt = 0.8/MaxConduct;
 
 extern double t;
 extern Matrix X;
 extern int Xnodes,Ynodes;
// extern Matrix DirectionX,DirectionY;
 double Length=1e-15;
 double OutPut = 0.0;
 double CoeLeak = 0.0;
// extern Matrix BloodVesselIndicatorX;
// extern Matrix BloodVesselIndicatorY;//So, BloodVessel is also important.
 //cout<<MaxConduct<<"MaxConduct"<<endl;
 double Uij = *Solution(i,j);
 double Ui_jp1 = 0;
 double Ui_jm1 = 0;
 double Uim1_j = 0;
 double Uip1_j = 0;

 double CoeffYell = 0;
 double CoeffYr = 0;
 double CoeffXell = 0;
 double CoeffXr   = 0; 

  if(min_abs(*BloodVesselIndicatorX(i,j))+min_abs(*BloodVesselIndicatorY(i,j))>0.5)
  {
  ////////
  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)	
  {
	  if(i==0)
	  {
	  CoeffXell =0;
	  
	  }
	  else
	  {
	  CoeffXell = *ConductX(i,j);//(*ConductX(i,j)+*ConductX(i-1,j))/2 ;
	  }
	  
	  if(i==Xnodes-1)
	  {
	  CoeffXr   = 0 ;
	  }
	  else
	  {
	  CoeffXr   = *ConductX(i+1,j) ;
	  }
	  if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
	  {
	   if(j==0)
	   {
	   CoeffYell = 0;//(*ConductY(i,j)+*ConductY(i,j))/2 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j);//(*ConductY(i,j)+*ConductY(i,j))/2 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = 0;
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j);
	   CoeffYr   = *ConductY(i,j+1);
	   }
	  }
	  else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
	  {
	  
	  }
	  else if(*BloodVesselIndicatorY(i,j)>0)
	  {
	  	if(j==0)
	   {
	   
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   
	   CoeffYr   = 0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   
	   CoeffYr   = *ConductY(i,j+1); 
	   }
	  }
	  else
	  {
	  	if(j==0)
	   {
	   CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j);
	   
	   }
	   else
	   {
	   CoeffYell =  *ConductY(i,j);	  
	   }
	  }
  }
  else if(min_abs(*BloodVesselIndicatorX(i,j))<0.5)
  {
  	  if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
	  {
	   if(j==0)
	   {
	   CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = *ConductY(i,j+1) ;
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   = 0 ;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   CoeffYell =  *ConductY(i,j) ;
	   CoeffYr   = *ConductY(i,j+1) ;
	   }
	  }
	  else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
	  {
	  
	  }
	  else if(*BloodVesselIndicatorY(i,j)>0)
	  {
	  	if(j==0)
	   {
	   
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   
	   CoeffYr   = 0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   
	   CoeffYr   =*ConductY(i,j+1); 
	   }
	  }
	  else
	  {
	  	if(j==0)
	   {
	   CoeffYell = 0;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j);
	   
	   }
	   else
	   {
	    CoeffYell = *ConductY(i,j);
	  
	   }
	  }
  }
  else if(*BloodVesselIndicatorX(i,j)>0)
  {

	  if(i==Xnodes-1)
	  {
	  CoeffXr   = 0 ;
	  }
	  else
	  {
	  CoeffXr   = *ConductX(i+1,j); 
	  }
      
       if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
	  {
	   if(j==0)
	   {
	   CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   =0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   = *ConductY(i,j+1) ; 
	   }
	  }
	  else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
	  {
	  
	  }
	  else if(*BloodVesselIndicatorY(i,j)>0)
	  {
	  	if(j==0)
	   {
	   
	   CoeffYr   = *ConductY(i,j+1) ;
	   }
	   else if(j==Ynodes-1)
	   {
	   
	   CoeffYr   = 0 ;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   
	   CoeffYr   = *ConductY(i,j+1) ; 
	   }
	  }
	  else
	  {
	  	if(j==0)
	   {
	   CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j) ;
	   
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j) ;
	  
	   }
	  }
  }
  else
  {
        if(i==0)
	  {
	  CoeffXell =0;
	  }
	  else
	  {
	  CoeffXell = *ConductX(i,j) ;
	  }

	
       if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
	  {
	   if(j==0)
	   {
	   CoeffYell = 0;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = *ConductY(i,j+1) ;
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   = 0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   = *ConductY(i,j+1) ; 
	   }
	  }
	  else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
	  {
	  
	  }
	  else if(*BloodVesselIndicatorY(i,j)>0)
	  {
	  	if(j==0)
	   {
	   
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   
	   CoeffYr   = 0 ;//(*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   
	   CoeffYr   = *ConductY(i,j+1) ; 
	   }
	  }
	  else
	  {
	  	if(j==0)
	   {
	   CoeffYell = 0;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j);
	   
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j);
	  
	   }
	  }
  }
   
	
	 ////// 
	 if(i==0)
     {
		Uim1_j = *Solution(i,j); 
	 }
   else
     {
		Uim1_j = *Solution(i-1,j);  
     }
   if(i==Xnodes-1)
     {
		Uip1_j = *Solution(i,j);
     }
   else
     {
		Uip1_j = *Solution(i+1,j);
     }

	 
	 
   if(j==0)
     {
		Ui_jm1 = *Solution(i,j); //Ui_jm1 = *Solution(i,Ynodes-1);
	 }
   else
     {
		Ui_jm1 = *Solution(i,j-1);  
     }
   if(j==Ynodes-1)
     {
		Ui_jp1 = *Solution(i,j);//Ui_jp1 = *Solution(i,0);
     }
   else
     {
		Ui_jp1 = *Solution(i,j+1);
     }
	
	 
	//if(j!=Ynodes-1)
	//{
	//  cout<<i<<","<<j<<*BloodVesselIndicatorX(i,j)<<"  "<<*BloodVesselIndicatorY(i,j)<<endl;
	//  cout<<"CoeffXell"<<CoeffXell<<"CoeffXr"<<CoeffXr<<endl;
	  
	 // cout<<"CoeffYell"<<CoeffYell<<"CoeffYr"<<CoeffYr<<endl;
	 
	
	//}  
	
 OutPut =  1.85*((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr) )-0.85*Uij;   
 if(!(min_abs(OutPut)<100000000.0))
 {
 cout<<OutPut<<"i  "<<i<<"j   "<<j<<" Uim1_j "<< Uim1_j<<" Uip1_j "<< Uip1_j<<" Ui_jm1 "<< Ui_jm1<<" Ui_jp1 "<< Ui_jp1<<endl;
 cout<<" left "<< CoeffXell<<" right "<< CoeffXr<<" down "<<CoeffYell<<" up "<<CoeffYr; 
 getchar();
 }	 
  
  
  return OutPut;
  }
  else// single point
  {
  return Uij;
  }
   
}






void VesselStructure::UpdateVesselSystem(void)
{
 cout <<"function UpdateVesselSystem running"<<endl; 
 extern int Xnodes, Ynodes;
 extern Matrix BloodVesselIndicatorX;
 extern Matrix BloodVesselIndicatorY;
 extern Matrix BloodVesselRadius;
 //extern Matrix BloodVesselFlowRate;
 extern Matrix SproutTips;
 extern Matrix SproutAge;
 extern Matrix VesselAge;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodNodePressure;
 extern Matrix BloodVesselPressureX;
	extern Matrix BloodVesselPressureY;
// extern Matrix DirectionX, DirectionY;
 extern Matrix FlowX, FlowY;
 extern Matrix LymIndicator;
 extern Matrix WssX, WssY;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern double GrowthDuration;
 GrowthDuration = RetrieveDoubleParameter( ParameterFile , "GrowthDuration");
 extern double FlowDuration;
 FlowDuration = RetrieveDoubleParameter( ParameterFile , "FlowDuration");
 LymIndicator=zeros(Xnodes, Ynodes);
 if( SproutNumber == 0 )
{
 cout << "Warning: All sprouts are dead! " << endl << endl;
}
cout << "sprout count: " << SproutNumber << endl;
 
if(!FlowBegin)
{
 //AngioUpdateMDE();
 //AngioUpdateECM();
 //AngioUpdateTAF();
 GetMaxTAF();
 GrowSprout();
 GrowSprout();
 GrowSprout();
 GrowSprout();
 GrowSprout();
 WSSTAFBranchByProbInterval();
}
else
{

 //AngioUpdateMDE();
 //AngioUpdateECM();
 //AngioUpdateTAF();
 GetMaxTAF();
 GrowSprout();
 WSSTAFBranchByProbInterval();
 VesselAdaption();
 TransOutPut();
 RegenerateFlow();


}



cout<<"Length:"<<VesselListLength<<endl;
cout <<"function UpdateVesselSystem finished"<<endl; 
}


void VesselStructure::UpdateVesselSystemSteadyState(void)
{
 cout <<"function UpdateVesselSystemSteadyState running"<<endl; 
 extern int Xnodes, Ynodes;
 extern Matrix BloodVesselIndicatorX;
 extern Matrix BloodVesselIndicatorY;
 extern Matrix BloodVesselRadius;
 //extern Matrix BloodVesselFlowRate;
 extern Matrix SproutTips;
 extern Matrix SproutAge;
 extern Matrix VesselAge;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodNodePressure;
 extern Matrix BloodVesselPressureX;
	extern Matrix BloodeVesselPressureY;
// extern Matrix DirectionX, DirectionY;
 extern Matrix FlowX, FlowY;
 extern Matrix Shuffle;
 extern Matrix WssX, WssY;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern double GrowthDuration;
 extern double FlowDuration;
 extern Matrix LymIndicator;
 int i=0;
 LymIndicator=zeros(Xnodes, Ynodes);
 
 if( SproutNumber == 0 )
{
 cout << "Warning: All sprouts are dead! " << endl << endl;
}
cout << "sprout count: " << SproutNumber << endl;
 
if(!FlowBegin)
{
 
 for(i=0;i<10;i++)  // Adaptionmodify
 {
 GrowSprout();
 AngioUpdateMDE();
 AngioUpdateECM();
 AngioUpdateTAF();
 GetMaxTAF();
 //WSSTAFBranchByProbInterval();
 TransOutPut();
RegenerateFlow();
 }
}
else
{
 
  for(i=0;i<10;i++)  //Adaptionmodify
 {
 GrowSprout();
 AngioUpdateMDE();
 AngioUpdateECM();
 AngioUpdateTAF();
 GetMaxTAF();
 WSSTAFBranchByProbInterval();
 }
 //BloodNodePressure = zeros(Xnodes, Ynodes);
 VesselAdaption();
 TransOutPut();
 RegenerateFlow();


}



cout<<"Length:"<<VesselListLength<<endl;
cout <<"function UpdateVesselSystemSteadyState finished"<<endl; 
}

void VesselStructure::UpdateVesselSystemSteadyStateShuffle(CellVesselStructure* TheVessel)
{
	cout <<"function UpdateVesselSystemSteadyState by pressure running"<<endl; 
	extern int Xnodes, Ynodes;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix LymIndicator;
	extern Matrix InterPressure;
	extern Matrix BloodVesselRadius;
	//extern Matrix BloodVesselFlowRate;
	extern Matrix SproutTips;
	extern Matrix SproutAge;
	extern Matrix VesselAge;
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodNodePressure;
	extern Matrix BloodVesselPressureX;
	extern Matrix BloodVesselPressureY;
	// extern Matrix DirectionX, DirectionY;
	extern Matrix FlowX, FlowY;
	extern Matrix TAF;
	extern Matrix WssX, WssY;
	extern Matrix VisX, VisY; //viscosity
	extern Matrix ConductX, ConductY; //conductivity
	extern double GrowthDuration;
	extern double FlowDuration;
	int i=0;
	LymIndicator=zeros(Xnodes, Ynodes);
	
	//cout << "UVSSSS begin, flowbegin: " << FlowBegin << endl;
	//getchar();
	
	if( SproutNumber == 0 )
	{
		cout << "Warning: All sprouts are dead! " << endl << endl;
	}
	cout << "sprout count: " << SproutNumber << endl;
	//cout << "flowbegin = " << FlowBegin << endl;
	if(!FlowBegin)
	{
		
		//Remodel(TheVessel);
		GetMaxTAF();
		TheVessel->ShuffleFate(TAF);
		TheVessel->ColorCell();
		TheVessel->SetSprout(TAF);
		GrowSprout_onecell(TheVessel);
		GrowSprout_onecell(TheVessel);
		GrowSprout_onecell(TheVessel);
		GrowSprout_onecell(TheVessel);
		Retract(TheVessel);
		Retract(TheVessel);
		Retract(TheVessel);
		

		//GrowSprout(TheVessel);
		//AngioUpdateMDE();
		//AngioUpdateECM();
		//AngioUpdateTAF();
		// GetMaxTAF();
		//cout << "I am starting again, haha" << endl;
		//WSSTAFBranchByProbInterval(Shuffle);
		TransOutPut();
		// RegenerateHem();
		MakeList();
		
	}
	else
	{
	    //Remodel(TheVessel);
	    UpdateStability();
		GetMaxTAF();
		TheVessel->ShuffleFate(TAF);
		TheVessel->ColorCell();
		TheVessel->SetSprout(TAF);
		
		//AngioUpdateTAF();
		GrowSprout_onecell(TheVessel);
		GrowSprout_onecell(TheVessel);
		GrowSprout_onecell(TheVessel);
		GrowSprout_onecell(TheVessel);
		//GrowSprout(TheVessel);
		//AngioUpdateMDE();
		//AngioUpdateECM();
		Retract(TheVessel);
		Retract(TheVessel);
		Retract(TheVessel);
		
		
		//WSSTAFBranchByProbInterval(Shuffle);
		
		//BloodNodePressure = zeros(Xnodes, Ynodes);
		VesselAdaption();
		
		TransOutPut();
		// RegenerateHem();
		MakeList();
		
		
	}
	
	
	
	cout<<"Length:"<<VesselListLength<<endl;
	cout <<"function UpdateVesselSystemSteadyState by pressure finished"<<endl; 
}


void VesselStructure::UpdateVesselSystemSteadyStateShuffle(void)
{
 cout <<"function UpdateVesselSystemSteadyState by pressure running"<<endl; 
 extern int Xnodes, Ynodes;
 extern Matrix BloodVesselIndicatorX;
 extern Matrix BloodVesselIndicatorY;
 extern Matrix LymIndicator;
 extern Matrix InterPressure;
 extern Matrix BloodVesselRadius;
 //extern Matrix BloodVesselFlowRate;
 extern Matrix SproutTips;
 extern Matrix SproutAge;
 extern Matrix VesselAge;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodNodePressure;
 extern Matrix BloodVesselPressureX;
	extern Matrix BloodVesselPressureY;
// extern Matrix DirectionX, DirectionY;
 extern Matrix FlowX, FlowY;
 extern Matrix TAF;
 extern Matrix WssX, WssY;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern double GrowthDuration;
 extern double FlowDuration;
 extern Matrix Shuffle;
 extern bool VesselBroke;
 int i=0;
 LymIndicator=zeros(Xnodes, Ynodes);
 
 //cout << "UVSSSS begin, flowbegin: " << FlowBegin << endl;
 //getchar();
 
 if( SproutNumber == 0 )
{
 cout << "Warning: All sprouts are dead! " << endl << endl;
}
cout << "sprout count: " << SproutNumber << endl;
 //cout << "flowbegin = " << FlowBegin << endl;
if(!FlowBegin)
{
 for(i=0;i<10;i++)  // Adaptionmodify
 {
 GrowSprout();
 AngioUpdateMDE();
 AngioUpdateECM();
 AngioUpdateTAF();
 GetMaxTAF();
 //cout << "I am starting again, haha" << endl;
 WSSTAFBranchByProbInterval(Shuffle);
 TransOutPut();
 RegenerateFlow();
 }

}
else
{
 /*
 if(AdaptionBegin)
 {
 GrowSprout(Shuffle);
 }
 else
 {
 GrowSprout();
 }
 AngioUpdateMDE();
 AngioUpdateECM();
 AngioUpdateTAF();
 GetMaxTAF();
 //cout << "I am starting ....." << endl;
 WSSTAFBranchByProbInterval(Shuffle);
  for(i=1;i<10;i++)  //Adaptionmodify
 {
 if(AdaptionBegin)
 {
 GrowSprout(Shuffle);
 }
 else
 {
 GrowSprout();
 }
 AngioUpdateMDE();
 AngioUpdateECM();
 AngioUpdateTAF();
 GetMaxTAF();
// WSSTAFBranchByProbInterval(Shuffle);
*/
  
  GrowSprout(Shuffle);

  if(VesselBroke)//add on Sep 22,2009
  {
    VesselAdaption_R();
  }    
  GrowSprout();
 //AngioUpdateMDE();
 //AngioUpdateECM();
 //AngioUpdateTAF();
 GetMaxTAF();
 

 
 WSSTAFBranchByProbInterval(Shuffle);
 //BloodNodePressure = zeros(Xnodes, Ynodes);
 VesselAdaption();

  TransOutPut();
 RegenerateFlow();
MakeList();
}



cout<<"Length:"<<VesselListLength<<endl;
cout <<"function UpdateVesselSystemSteadyState by pressure finished"<<endl; 
}

void VesselStructure::UpdateVesselSystem_R()
{
 cout <<"function UpdateVesselSystemSteadyState by pressure running"<<endl; 
 extern int Xnodes, Ynodes;
 extern Matrix BloodVesselIndicatorX;
 extern Matrix BloodVesselIndicatorY;
 extern Matrix LymIndicator;
 extern Matrix InterPressure;
 extern Matrix BloodVesselRadius;
 //extern Matrix BloodVesselFlowRate;
 extern Matrix SproutTips;
 extern Matrix SproutAge;
 extern Matrix VesselAge;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodNodePressure;
 extern Matrix BloodVesselPressureX;
 extern Matrix Shuffle;
// extern Matrix DirectionX, DirectionY;
 extern Matrix FlowX, FlowY;
 extern Matrix TAF;
 extern Matrix WssX, WssY;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern double GrowthDuration;
 extern double FlowDuration;
 int i=0;
 LymIndicator=zeros(Xnodes, Ynodes);
 Proliferate();
 //CellDiffuse();
 GetMaxTAF();
// if(MaxTAF)
// {
// Shuffle = zeros(Xnodes, Ynodes);
// }
 GrowSprout_one_step();

 VesselAdaption_R();
 TransOutPut();//GetFlow
 
 WSSTAFBranchByProbInterval_R();
 RegenerateFlow();
 //UpdateRadius_Flow();
 UpdateVEGFR();
 UpdateNotch();
 UpdateDelta4();
cout<<"VesselPointLength:"<<VesselListLength<<endl;
cout<<"StructureCellNumber:"<<CellNumber<<endl;
cout<<"SproutTipNumber:"<<SproutNumber<<endl;
cout <<"function UpdateVesselSystem_R by pressure finished"<<endl; 
}


void AngioUpdateMDE(void)
{

//cout <<"function AngioUpdateMDE running"<<endl; 
extern Matrix MDE;
extern Matrix SproutTips;

extern int Xnodes, Ynodes;
double AngiogenesisAlpha = RetrieveDoubleParameter( ParameterFile, "AngiogenesisAlpha");
for (int j = 0; j < Ynodes; j++)
 {
for(int i = 0; i < Xnodes; i++)
  {
if(*SproutTips(i,j) > 0) 
   {
    *MDE(i,j) = *MDE(i,j) + AngioDeltaT * AngiogenesisAlpha * *SproutTips(i,j);
   }
  }
 }
//cout <<"function AngioUpdateMDE finished"<<endl; 
}

void AngioUpdateECM(void)
{
//cout <<"function AngioUpdateECM running"<<endl; 
extern Matrix ECM;
extern Matrix MDE;
extern Matrix SproutTips;
extern double ECM_DegradationConstant;

extern int Xnodes, Ynodes;
double beta= RetrieveDoubleParameter( ParameterFile , "beta"); // 0.05;
double gamma= RetrieveDoubleParameter( ParameterFile , "gamma");
for (int j = 0; j < Ynodes; j++)
 {
for(int i = 0; i < Xnodes; i++)
  {
  
   *ECM(i,j) = *ECM(i,j) - AngioDeltaT * gamma * *ECM(i,j) * *MDE(i,j);
if(*SproutTips(i,j) > 0) 
   {
   *ECM(i,j) = *ECM(i,j) + AngioDeltaT * beta * *SproutTips(i,j);
	//*ECM(i,j) = *ECM(i,j) + AngioDeltaT * ( beta -  ECM_DegradationConstant * *ECM(i, j) ) ;
   }
   
  }
 }
// //cout <<"function AngioUpdateECM finished"<<endl; 

}

void AngioUpdateTAF(void)
{
//cout <<"function AngioUpdateECM running"<<endl; 
extern Matrix TAF;
extern Matrix BloodVesselIndicator;
extern double EndothelialTAFuptakeRate;

extern int Xnodes, Ynodes;
for (int j = 0; j < Ynodes; j++)
 {
for(int i = 0; i < Xnodes; i++)
  {
  
if(*BloodVesselIndicator(i,j) > 0) 
   {
    //COMBINE *ECM(i,j) = *ECM(i,j) + AngioDeltaT * beta * *SproutTips(i,j);
	*TAF(i,j) = *TAF(i,j) - *BloodVesselIndicator(i,j)*AngioDeltaT * EndothelialTAFuptakeRate;
   }
   if(*TAF(i,j)<=0)
   {
   *TAF(i,j) = 0;
   }
   
  }
 }
// //cout <<"function AngioUpdateECM finished"<<endl; 

}

void GetMaxTAF()
{
	//cout <<"function GetMaxTAF running"<<endl; 

	extern Matrix TAF;
	
	MaxTAF = 1;
	extern int Xnodes;
	extern int Ynodes;
	/*
	for(int i = 0; i< Xnodes; i++)
	{
		for(int j = 0; j< Ynodes; j++)
		{
			if (*TAF(i, j) > MaxTAF)
			{
				MaxTAF = *TAF(i, j);
			}
		}
	}
	//cout <<"function GetMaxTAF finished"<<endl; 
*/

}
void VesselStructure::GrowSprout(CellVesselStructure* TheVessel)
{
	cout <<"function GrowSprout running"<<endl; 
	extern bool VesselBroke;
	extern int touchpoints;
	extern Matrix SproutTips;
	extern Matrix SproutAge;
	extern Matrix VesselAge;
	//extern Matrix DirectionX;
	//extern Matrix DirectionY;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix BloodVesselIndicator;
	extern Matrix TAF;
	extern Matrix ECM;
	extern Matrix CellColor;
	extern int Xnodes, Ynodes;
	double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
	double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
	double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
	double String = RetrieveDoubleParameter( ParameterFile , "String");
	double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
	AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
	AngioDeltaY = 1.0 /Ynodes;
	Matrix Prob0 = zeros(Xnodes, Ynodes);
	Matrix Prob1 = zeros(Xnodes, Ynodes);
	Matrix Prob2 = zeros(Xnodes, Ynodes);
	Matrix Prob3 = zeros(Xnodes, Ynodes);
	Matrix Prob4 = zeros(Xnodes, Ynodes);
	Matrix Inf0 = zeros(Xnodes, Ynodes);
	Matrix Inf1 = zeros(Xnodes, Ynodes);
	Matrix Inf2 = zeros(Xnodes, Ynodes);
	Matrix Inf3 = zeros(Xnodes, Ynodes);
	Matrix Inf4 = zeros(Xnodes, Ynodes);
	Matrix Sup0 = zeros(Xnodes, Ynodes);
	Matrix Sup1 = zeros(Xnodes, Ynodes);
	Matrix Sup2 = zeros(Xnodes, Ynodes);
	Matrix Sup3 = zeros(Xnodes, Ynodes);
	Matrix Sup4 = zeros(Xnodes, Ynodes);
	int iminus,iplus,jminus,jplus;
	bool growstill=1;
	bool setorigin=0;
	int LoopLength = VesselListLength; 
	cout<<"Growth Begin"<<endl;
	VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
	VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
	VesselCell* pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
	VesselCell* pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
	VesselCell* pTempVesselCell = TheVessel->pFirstVesselCell;
	
	while(growstill || !setorigin)
	{
		
		cout<<"Once Begin"<<endl;
		growstill = 0;
		
		
		Prob0 = zeros(Xnodes, Ynodes);
		Prob1 = zeros(Xnodes, Ynodes);
		Prob2 = zeros(Xnodes, Ynodes);
		Prob3 = zeros(Xnodes, Ynodes);
		Prob4 = zeros(Xnodes, Ynodes);
		Inf0 = zeros(Xnodes, Ynodes);
		Inf1 = zeros(Xnodes, Ynodes);
		Inf2 = zeros(Xnodes, Ynodes);
		Inf3 = zeros(Xnodes, Ynodes);
		Inf4 = zeros(Xnodes, Ynodes);
		Sup0 = zeros(Xnodes, Ynodes);
		Sup1 = zeros(Xnodes, Ynodes);
		Sup2 = zeros(Xnodes, Ynodes);
		Sup3 = zeros(Xnodes, Ynodes);
		Sup4 = zeros(Xnodes, Ynodes);
		
		
		pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
		pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
		pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
		pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
		pTempVesselCell = TheVessel->pFirstVesselCell;
		LoopLength = VesselListLength;
		VesselBroke=0;
		
		
		for(int Loop = 0; Loop < LoopLength; Loop++)
		{
			//cout<<"enter loop"<<endl;
			int i = pCurrentVesselPoint1->i;
			int j = pCurrentVesselPoint1->j;
			int i_origin = pCurrentVesselPoint1->i_origin;
			int j_origin = pCurrentVesselPoint1->j_origin;
			
			
			
			if((i==0||j==0||i==Xnodes-1||j==Ynodes-1)&&*SproutTips(i,j)==1)
			{
				*SproutTips(i,j)=0;
				//SproutNumber -=1;
			}
			
			
			if( *SproutTips(i,j) > 0)
			{
				
				
				//cout<<"enter sprouttip loop"<<endl;
				//cout<<"beginsprout";
				if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
				if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
				if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
				if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
				
				//Prob0 should be with stability 
				
				/*
				 *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i, j) );
				 
				 *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i, j) );
				 
				 *Prob3(i, j) = Chemo_Response*( *TAF(i, jplus) - *TAF(i, j) );	   
				 
				 *Prob4(i, j) = Chemo_Response*( *TAF(i, jminus) - *TAF(i, j) );
				 */
				*Prob0(i, j) = 0;
				*Prob1(i, j) = 0;
				*Prob2(i, j) = 0;
				*Prob3(i, j) = 0;
				*Prob4(i, j) = 0;
				if( *TAF(iplus, j) - *TAF(i, j) > 0.0)
				{
					*Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i_origin, j_origin) ) - String*pow((iplus-i_origin)*(iplus-i_origin)+(j-j_origin)*(j-j_origin), 0.5);
					// cout<< "*Prob1(i, j)"<<*Prob1(i, j)<<endl;
				}
				if(*TAF(iminus, j) - *TAF(i, j) > 0.0)
				{
					*Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i_origin, j_origin) ) - String*pow((iminus-i_origin)*(iminus-i_origin)+(j-j_origin)*(j-j_origin), 0.5);
					//	  cout<< "*Prob2(i, j)"<<*Prob2(i, j)<<endl;
				}
				if(*TAF(i, jplus) - *TAF(i, j) > 0.0)
				{
					//	cout<< "*Prob3(i, j)"<<*Prob3(i, j)<<endl;
					*Prob3(i, j) = Chemo_Response*( *TAF(i,jplus) - *TAF(i_origin, j_origin) )  - String*pow((i-i_origin)*(i-i_origin)+(jplus-j_origin)*(jplus-j_origin), 0.5);	   
				}
				if(*TAF(i, jminus) - *TAF(i, j) > 0.0)
				{
					
					//	cout<< "*Prob4(i, j)"<<*Prob4(i, j)<<endl;
					*Prob4(i, j) = Chemo_Response*(  *TAF(i,jminus) - *TAF(i_origin, j_origin) ) - String*pow((i-i_origin)*(i-i_origin)+(jminus-j_origin)*(jminus-j_origin), 0.5);
				}
				
				
				
				
				
				if( *Prob0(i,j) <= 0 )
				{
					*Prob0(i,j) = 0;
				}
				if( *Prob1(i,j) <= 0 )
				{
					*Prob1(i,j) = 0;
					
				}
				if( *Prob2(i,j) <= 0 )
				{
					*Prob2(i,j) = 0;
					
				}
				if( *Prob3(i,j) <= 0 )
				{
					*Prob3(i,j) = 0;
					
				}
				
				if( *Prob4(i,j) <= 0 )
				{
					*Prob4(i,j) = 0;
					
				}
				
				
				
				
				
				//the sprout shouldn't grow reverse  
				if (*BloodVesselIndicatorX(i,j) < 0 )
				{
					*Prob2(i,j) = 0;
				}
				else if (*BloodVesselIndicatorX(i,j) > 0)
				{
					*Prob1(i,j) = 0;
				}
				else if (*BloodVesselIndicatorY(i,j) < 0 )
				{
					*Prob4(i,j) = 0;
				}
				else if (*BloodVesselIndicatorY(i,j) > 0 )
				{
					*Prob3(i,j) = 0;
				}
				//other exceptions 
				if (i == Xnodes-1) 
				{ *Prob1(i, j) = 0.0; }
				else if (i == 0)   
				{ *Prob2(i, j) = 0.0; }
				if (j == ( Ynodes-1))
				{ *Prob3(i, j) = 0.0; }
				if (j == 0)
				{ *Prob4(i, j) = 0.0; }
				
				
				// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
				//rescale the probabilities
				double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
				if ( ProbTotal> 0 )
				{
					growstill = 1;
					
					*Prob0(i,j) /= ProbTotal;
					*Prob1(i,j) /= ProbTotal;
					*Prob2(i,j) /= ProbTotal;
					*Prob3(i,j) /= ProbTotal;
					*Prob4(i,j) /= ProbTotal;
					
					cout<<*Prob0(i,j)<<"  "<<*Prob1(i,j)<<"  " <<*Prob2(i,j)<<"  "<<*Prob3(i,j)<<"  " <<*Prob4(i,j)<<endl;
					
					//Get Random Intervals
					*Inf0(i,j) = 0;
					*Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
					//*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
					*Inf1(i,j) = *Sup0(i,j);
					*Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
					//*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
					*Inf2(i,j) = *Sup1(i,j);
					*Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
					*Inf3(i,j) = *Sup2(i,j);
					*Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
					//*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
					*Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
					*Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
					//*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
					
					//Done with those probabilities, begin to Move and Connect in one step.
					double RandNumber;
					int I, J;
					RandNumber = float( rand() ) / float( RAND_MAX);
					
					if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
					{
						
					}
					
					else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
					{
						I = i + 1;
						J = j;
						
						
						if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
								SproutNumber -= 1;//comment this we have anasmosis
								
							}
							
							
							
							//*DirectionX(I, J) = 1;
							//*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
							//SproutNumber -=1;//Add this we have anasmosis
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;
							*SproutTips(i, j) = 0;	
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
							
							
							
							
						}
						else
						{
							
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionX(I, J) = 1;
							
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
							
							
							
						}
						
						
						
						if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
						{
							*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorX(i,j)<-0.5)
							{*BloodVesselIndicatorX(i,j)=2;}
							else
							{*BloodVesselIndicatorX(i,j)=1;}
						}
						if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
						{
							*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorX(I,J)>0.5)
							{*BloodVesselIndicatorX(I,J)=2;}
							else
							{*BloodVesselIndicatorX(I,J)=-1;}
						}
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
						
						
					}
					
					else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
					{
						I = i - 1;
						J = j;
						
						
						if(  (min_abs(*BloodVesselIndicator(I, J))) > 0 )
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;	
								
								
							}
							
							
							// *DirectionX(I, J) = -1;
							// *SproutTips(I, J) = 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	
							//SproutNumber -= 1;
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob2 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionX(I, J) = -1;
							
							
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
							
						}
						
						
						if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
						{
							*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorX(i,j)>0.5)
							{*BloodVesselIndicatorX(i,j)=2;}
							else
							{*BloodVesselIndicatorX(i,j)=-1;}
						}
						if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
						{
							*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorX(I,J)<-0.5)
							{*BloodVesselIndicatorX(I,J)=2;}
							else
							{*BloodVesselIndicatorX(I,J)= 1;}
						}
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
					{
						I = i;
						J = j + 1;
						
						if( (*BloodVesselIndicator(I, J))> 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;
								
								
							}
							
							
							
							// SproutNumber -= 1;
							// *DirectionY(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	 
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob3 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionY(I, J) = 1;
							
							
							//cout<<"here"<<endl;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							//cout<<pCurrentVesselCell1->cIndex<<endl;
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//cout<<pCurrentVesselCell2->cIndex<<endl;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
						}
						
						if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
						{
							*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorY(i,j)<-0.5)
							{*BloodVesselIndicatorY(i,j)=2;}
							else
							{*BloodVesselIndicatorY(i,j)=1;}
						}
						if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
						{
							*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorY(I,J)>0.5)
							{*BloodVesselIndicatorY(I,J)=2;}
							else
							{*BloodVesselIndicatorY(I,J)=-1;}
						}
						
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
					{
						I = i;
						J = j - 1;
						
						
						if( (*BloodVesselIndicator(I, J)) > 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;
							}
							
							
							// *DirectionY(I, J) = -1;
							// *SproutTips(I, J) = 1;
							//SproutNumber -= 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{		
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob4 make Vessel Length"<<VesselListLength<<endl;
							
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionY(I, J) = -1;
							
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
						}
						
						if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
						{
							*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorY(i,j)>0.5)
							{*BloodVesselIndicatorY(i,j)=2;}
							else
							{*BloodVesselIndicatorY(i,j)=-1;}
						}
						if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
						{
							*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorY(I,J)<-0.5)
							{*BloodVesselIndicatorY(I,J)=2;}
							else
							{*BloodVesselIndicatorY(I,J)=1;}
						}
						
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					//BOUNDARY SPROUTS We can ask it to be periodical?
				}//probtotal>0
				else
				{
					//cout<<"Show if i come here"<<endl;
					//getchar();
					*SproutTips(i,j) = 0;
				}//probtotal=0
				
			}//this is for the if sprout
			
			
			pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
			//cout<<"aftermove";
		}//this is for the loop
		pLastVesselPoint = pCurrentVesselPoint2;
		TheVessel->pLastVesselCell = pCurrentVesselCell2;
		//QuickPlot();// COMBINE
		cout <<"GrowSprout once"<<endl; 
		if(!growstill)
		{
			
			pCurrentVesselPoint1 = pFirstVesselPoint;
			
			
			
			while(pCurrentVesselPoint1)
			{
				pCurrentVesselPoint1->i_origin = pCurrentVesselPoint1->i;
				pCurrentVesselPoint1->j_origin = pCurrentVesselPoint1->j;
				
				
				
				pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
			}
			
			
			
			
			setorigin = 1;
			
			
			
			
			cout <<"origin reset"<<endl; 
			
		}//setorigin 
		
		
		
	}//still loop
	cout <<"function GrowSprout finish"<<endl;
}


void VesselStructure::GrowSprout()
{
//cout <<"function GrowSprout running"<<endl; 

extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix TAF;
extern Matrix ECM;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
//sweep the list to update rather than matrix?
//yes!
//should we have more list?
//we don't need to, because even if we have two main vessel,
//it's like a whole system with the general BC and IC.

//DON'T FORGET THE INDICATOR
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
int LoopLength = VesselListLength; 
for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"enter loop"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
 // cout<<i<<"    "<<j<<endl;
 
    
	if(i==1&&*SproutTips(i,j)==1)
	{
	*SproutTips(i,j)=0;
	SproutNumber -=1;
	}
	else if(i==Xnodes-2&&*SproutTips(i,j)==1)
	{
	*SproutTips(i,j)=0;
	SproutNumber -=1;
	}
	if( *SproutTips(i,j) > 0)
	{
	//cout<<"enter sprouttip loop"<<endl;
	    if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
		
	       /****  			 
		*Prob0(i, j) = 1.0
	                 - 2.0 * Dif_ECM * AngioDeltaT * ( (1.0/pow(AngioDeltaX,2) ) + (1.0/pow(AngioDeltaY,2) ) )
					 + ( ( Chemo_Response * Chemo_Sense * AngioDeltaT ) / pow( ( 1.0 + Chemo_Sense * *TAF(i,j) ), 2) ) 
						* ( (pow( ( *TAF(iplus, j) - *TAF(iminus, j) ), 2) / pow( (2.0*AngioDeltaX), 2) )
	                       +(pow( ( *TAF(i, jplus) - *TAF(i, jminus) ), 2) / pow( (2.0*AngioDeltaY), 2) ) ) 
                     - ( (Chemo_Response * AngioDeltaT) / ( 1.0 + Chemo_Sense* *TAF(i, j) ) )
	                    *( ( ( *TAF(iplus, j) - 2.0 * *TAF(i, j) + *TAF(iminus, j) ) / pow(AngioDeltaX, 2) )
                          +( ( *TAF(i, jplus) - 2.0 * *TAF(i, j) + *TAF(i, jminus) ) / pow(AngioDeltaY, 2) ) )
	                 - Haptotactic_Reponse * AngioDeltaT * ( ( ( *ECM(iplus, j) - 2.0 * *ECM(i, j) + *ECM(iminus, j) ) / pow(AngioDeltaX, 2) )
	                                         +( ( *ECM(i, jplus) - 2.0 * *ECM(i, j) + *ECM(i, jminus) ) / pow(AngioDeltaY, 2) ) );
											 
	  
		  *Prob1(i, j) = ( Dif_ECM * AngioDeltaT ) / pow( AngioDeltaX, 2 )
						+ ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(iplus, j) - *TAF(i, j) )
		                      + Haptotactic_Reponse * ( *ECM(iplus, j) - *ECM(i, j) ) );
		
		


	 
	
	     *Prob2(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaX, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, j) - *TAF(iminus, j) )
		                      + Haptotactic_Reponse * ( *ECM(i, j) - *ECM(iminus, j) ) );
	
	
	
	     *Prob3(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       + ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, jplus) - *TAF(i, j) )
		                      + Haptotactic_Reponse * ( *ECM(i, jplus) - *ECM(i, j) ) );
	
	   
    
	     *Prob4(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, j) - *TAF(i, jminus) )
		                      + Haptotactic_Reponse * ( *ECM(i, j) - *ECM(i, jminus) ) );
		  *********/
		 
		*Prob0(i, j) = 1.0
	                 - 2.0 * Dif_ECM * AngioDeltaT * ( (1.0/pow(AngioDeltaX,2) ) + (1.0/pow(AngioDeltaY,2) ) )
					 + ( ( Chemo_Response * Chemo_Sense * AngioDeltaT ) / pow( ( 1.0 + Chemo_Sense * *TAF(i,j) ), 2) ) 
						* ( (pow( ( *TAF(iplus, j) - *TAF(iminus, j) ), 2) / pow( (2.0*AngioDeltaX), 2) )
	                       +(pow( ( *TAF(i, jplus) - *TAF(i, jminus) ), 2) / pow( (2.0*AngioDeltaY), 2) ) ) 
                     - ( (Chemo_Response * AngioDeltaT) / ( 1.0 + Chemo_Sense* *TAF(i, j) ) )
	                    *( ( ( *TAF(iplus, j) - 2.0 * *TAF(i, j) + *TAF(iminus, j) ) / pow(AngioDeltaX, 2) )
                          +( ( *TAF(i, jplus) - 2.0 * *TAF(i, j) + *TAF(i, jminus) ) / pow(AngioDeltaY, 2) ) )
	                 - Haptotactic_Reponse * AngioDeltaT * ( ( ( *ECM(iplus, j) - 2.0 * *ECM(i, j) + *ECM(iminus, j) ) / pow(AngioDeltaX, 2) )
	                                         +( ( *ECM(i, jplus) - 2.0 * *ECM(i, j) + *ECM(i, jminus) ) / pow(AngioDeltaY, 2) ) );
											 
	  
		  *Prob1(i, j) = ( Dif_ECM * AngioDeltaT ) / pow( AngioDeltaX, 2 )
						+ ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(iplus, j) - *TAF(iminus, j) )
		                      + Haptotactic_Reponse * ( *ECM(iplus, j) - *ECM(iminus, j) ) );
		
		


	 
	
	     *Prob2(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaX, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(iplus, j) - *TAF(iminus, j) )
		                      + Haptotactic_Reponse * ( *ECM(iplus, j) - *ECM(iminus, j) ) );
	
	
	
	     *Prob3(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       + ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, jplus) - *TAF(i, jminus) )
		                      + Haptotactic_Reponse * ( *ECM(i, jplus) - *ECM(i, jminus) ) );
	
	   
    
	     *Prob4(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, jplus) - *TAF(i, jminus) )
		                      + Haptotactic_Reponse * ( *ECM(i, jplus) - *ECM(i, jminus) ) );
							  
							

   //cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 // make probabilities above 0  
	   double MakeUpToZero1, MakeUpToZero2, MakeUpToZero3, MakeUpToZero4;
	  
			MakeUpToZero1 = 0;
			MakeUpToZero2 = 0;
			MakeUpToZero3 = 0;
			MakeUpToZero4 = 0;
			
		if( *Prob0(i,j) < 0 )
		{
		*Prob0(i,j) = 0;
		}
		if( *Prob1(i,j) < 0 )
		{
		MakeUpToZero1 = min_abs(*Prob1(i,j));
	
		}
		if( *Prob2(i,j) < 0 )
		{
		MakeUpToZero2 = min_abs(*Prob2(i,j));
		
		}
		if( *Prob3(i,j) < 0 )
		{
		MakeUpToZero3 = min_abs(*Prob3(i,j));
		
		}
		if( *Prob4(i,j) < 0 )
		{
		MakeUpToZero4 = min_abs(*Prob4(i,j));
		
		}
       
	   *Prob1(i,j) += (MakeUpToZero1 + MakeUpToZero2);
	   *Prob2(i,j) += (MakeUpToZero1 + MakeUpToZero2);
	   *Prob3(i,j) += (MakeUpToZero3 + MakeUpToZero4);
	   *Prob4(i,j) += (MakeUpToZero3 + MakeUpToZero4);
	   
	   
	 //the sprout shouldn't grow reverse  
	   if (*BloodVesselIndicatorX(i,j) < 0 )
	   {
	   *Prob2(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorX(i,j) > 0)
	   {
	   *Prob1(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) < 0 )
	   {
	   *Prob4(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) > 0 )
	   {
	   *Prob3(i,j) = 0;
	   }
	   //other exceptions 
		if (i == Xnodes-1) 
	    { *Prob1(i, j) = 0.0; }
		else if (i == 0)   
	    { *Prob2(i, j) = 0.0; }
		if (j >= ( Ynodes-25))
	    { *Prob3(i, j) = 0.0; }
		 if (j == 0)
	    { *Prob4(i, j) = 0.0; }


   // cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
     //rescale the probabilities
	 double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
	 if ( ProbTotal> 0 )
	 {
		*Prob0(i,j) /= ProbTotal;
		*Prob1(i,j) /= ProbTotal;
		*Prob2(i,j) /= ProbTotal;
		*Prob3(i,j) /= ProbTotal;
		*Prob4(i,j) /= ProbTotal;
	 }
	// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 
	 //Get Random Intervals
	 *Inf0(i,j) = 0;
	 *Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
	 //*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
	 *Inf1(i,j) = *Sup0(i,j);
	 *Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
	 //*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
	 *Inf2(i,j) = *Sup1(i,j);
	 *Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
	 *Inf3(i,j) = *Sup2(i,j);
	 *Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
	 //*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
	 *Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
	 *Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
	 //*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
	 
	 //Done with those probabilities, begin to Move and Connect in one step.
	 
	 double RandNumber;
	 int I, J;
	 RandNumber = float( rand() ) / float( RAND_MAX);
	 if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
	 {
		I = i;
		J = j;
		*SproutAge(I, J) += AngioDeltaT;
	 }
	 
	 else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
	 {
		I = i + 1;
		J = j;
		
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
	     if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 *SproutTips(i, j) = 0;
		 //*DirectionX(I, J) = 1;
		 *SproutTips(I, J) = 0;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	 
		}
		else
		{
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = 1;
		}
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }
     
	 else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
	 {
		I = i - 1;
		J = j;
		
		if(  (min_abs(*BloodVesselIndicator(I, J))) > 0 )
		{
		
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			SproutNumber -= 1;	
			
	
		  }
		 
		  		  
		 *SproutTips(i, j) = 0;
		// *DirectionX(I, J) = -1;
		 *SproutTips(I, J) = 0;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		 //SproutNumber -= 1;
	 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob2 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = -1;
		}
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	 }
	 
	 else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
	 {
		I = i;
		J = j + 1;
		
		
		if( (*BloodVesselIndicator(I, J))> 0)
		{
		 
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
				SproutNumber -= 1;
				  

		  }
		 
			  
		 *SproutTips(i, j) = 0;
		// SproutNumber -= 1;
		// *DirectionY(I, J) = 1;
		 *SproutTips(I, J) = 0;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob3 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 0;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = 1;
		}
		
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	 }
	 
	 else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
	 {
		I = i;
		J = j - 1;
				
		if( (*BloodVesselIndicator(I, J)) > 0)
		{
		 
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			 SproutNumber -= 1;
		  }
		 		  
		 *SproutTips(i, j) = 0;
		// *DirectionY(I, J) = -1;
		 *SproutTips(I, J) = 1;
		 //SproutNumber -= 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		}
		else
		{		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob4 make Vessel Length"<<VesselListLength<<endl;

		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = -1;
		}
		
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	 }

	//BOUNDARY SPROUTS We can ask it to be periodical?
	
	 if(I == 0 || I == Xnodes - 1 || J == 0 || J == Ynodes - 1)
	 {
	  *SproutTips(I, J) = 0;
	  *SproutAge(I, J) = 0;
	 }
	 
	  // cout<<"quit sprouttip loop"<<endl;

	 
	 }//this is for the if
	 else
	 {
		*VesselAge(i,j) += AngioDeltaT; 
		//cout<<"Age"<<*VesselAge(i,j);
	 }
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   QuickPlot();// COMBINE
   //cout <<"function GrowSprout finished"<<endl; 
 }
 

void VesselStructure::VesselAdaption_R()
{

double Pin, Pout;
int Iteration = 50000;
double Tolerance = 0.001;//0.0001;If you change it, need change ptupdate as well

extern Matrix Permeability;
extern Matrix VesselAge;
extern Matrix Conduct;
extern Matrix WallGradient;
extern Matrix BloodNodePressure;
extern Matrix HaematocritLevel;
extern int Xnodes, Ynodes;
double MaxConduct;
 Permeability=ones(Xnodes, Ynodes);
 WallGradient=zeros(Xnodes, Ynodes);
UpdatePermeability();

Conduct =zeros(Xnodes, Ynodes);
extern Matrix X, Y;
extern Matrix LymIndicator;
Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
Pout = RetrieveDoubleParameter( ParameterFile , "Pout");
extern Matrix ConductX, ConductY;
extern int Xnodes, Ynodes;
cout <<"function VesselAdaption running"<<endl; 
extern bool AdaptionBegin;

//getchar();

//UpdateRadius();

 UpdateConductivity();

{
//ChainAngioPressureUpdate(Iteration, Tolerance);

 BloodNodePressure = zeros(Xnodes, Ynodes);
InterAngioPressureUpdate(Iteration, Tolerance, MaxConduct);
GetVesselPressureAndFlow(Tolerance);
UpdateWallShearStress();
HaematocritLevel=zeros(Xnodes, Ynodes);
SteadyStateHematocrit();

}

//for(int i=0;i<10;i++)
//{

UpdateViscosity();
//CleanUp();
//UpdateRadius();


//UpdateConductivity();


//}


   

cout <<"function VesselAdaption Repair finished"<<endl; 




}

void VesselStructure::VesselAdaption()
{

double Pin, Pout;
int Iteration = 50000;
double Tolerance = 0.001;//0.0001;If you change it, need change ptupdate as well

extern Matrix Permeability;
extern Matrix VesselAge;
extern Matrix Conduct;
extern Matrix WallGradient;
extern Matrix BloodNodePressure;
extern int Xnodes, Ynodes;
double MaxConduct;
 Permeability=ones(Xnodes, Ynodes);
 WallGradient=zeros(Xnodes, Ynodes);
UpdatePermeability();

Conduct =zeros(Xnodes, Ynodes);
extern Matrix X, Y;
extern Matrix LymIndicator;
Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
Pout = RetrieveDoubleParameter( ParameterFile , "Pout");
extern Matrix ConductX, ConductY;
extern int Xnodes, Ynodes;
cout <<"function VesselAdaption running"<<endl; 
extern bool AdaptionBegin;

//getchar();


UpdateConductivity();

//ChainAngioPressureUpdate(Iteration, Tolerance);

 //BloodNodePressure = zeros(Xnodes, Ynodes);
InterAngioPressureUpdate(Iteration, Tolerance);
GetVesselPressureAndFlow(Tolerance);
	SteadyStateHematocrit();
	UpdateViscosity();
UpdateWallShearStress();




//for(int i=0;i<10;i++)
//{



UpdateRadius();


//UpdateConductivity();


//}


   

cout <<"function VesselAdaption finished"<<endl; 


}


void VesselStructure::MindeUpdateHematocrit()
{
//cout <<"function UpdateHematocrit running"<<endl; 

 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodNodePressure;
 extern Matrix BloodVesselPressureX;
 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix FlowX, FlowY;
 extern Matrix WssX, WssY;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern int Xnodes, Ynodes;
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;


    double FlowMin=1.0e-30;
	double TimeStepMax= AngioDeltaT;
	int MinTime_i,MinTime_j;
	double pi=3.141592654;
	double TimeStep = AngioDeltaT;
	double TimeFactor;
	TimeFactor = 1.0;
	double MaxHt = RetrieveDoubleParameter( ParameterFile , "MaxHt");
	double AverageHt = RetrieveDoubleParameter( ParameterFile , "AverageHt");
	Matrix Flow;
	Matrix PassByMass;
	Flow = zeros( Xnodes, Ynodes);
	PassByMass = zeros( Xnodes, Ynodes);
	int i_minus, j_minus, i_plus, j_plus;
	int i, j;
	while(pCurrentVesselPoint)
	{
		 i = pCurrentVesselPoint->i;
		 j = pCurrentVesselPoint->j;
		
		//cout<<"here after"<<endl;
				//coue<"here before"<<endl;
		
		//cout<<*Flow(i,j)<<endl;
		//cout<<min_abs( *FlowX(i, j) )<<endl;
		
	//	cout<<*FlowX(i,j)<<endl;
	//	cout<<*FlowY(i,j)<<endl;
		if(min_abs( *FlowX(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusXdir(i, j), 2) * NodeLength / min_abs( *FlowX(i, j) );
			//cout<<"TimeStep"<<TimeStep<<endl;
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
				//cout<<"TimeStepMax"<<TimeStepMax<<endl;
			}
			
		}
		
		if(min_abs( *FlowY(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusYdir(i, j), 2) * NodeLength / min_abs( *FlowY(i, j) );
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
			}

		}
	  
	  pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
	}
	
	AngioDeltaT = TimeStepMax / TimeFactor;
	cout<<"AngioDeltaT"<<AngioDeltaT<<endl;
	
	pCurrentVesselPoint = pFirstVesselPoint;
	
	//get bypassmass
	while(pCurrentVesselPoint)
	{
		 i = pCurrentVesselPoint->i;
		 j = pCurrentVesselPoint->j;
		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if(*FlowX(i, j) > 0&&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
			*Flow(i, j) += min_abs( *FlowX(i, j) );

			*PassByMass(i, j) += min_abs( *FlowX(i, j) ) * *HaematocritLevel(i_minus, j) ;
			//cout<<*PassByMass(i, j)<<"ByMass"<<endl; 
	}
	
	if(*FlowX(i_plus, j)*(-1.0) > 0&&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
			*Flow(i, j) += min_abs( *FlowX(i_plus, j) );
			*PassByMass(i, j) += min_abs( *FlowX(i_plus, j) ) * *HaematocritLevel(i_plus, j) ;
			//cout<<*PassByMass(i, j)<<"ByMass"<<endl; 
	}
	
	if(*FlowY(i, j) > 0&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			*Flow(i, j) += min_abs( *FlowY(i, j) );
			*PassByMass(i, j) += min_abs( *FlowY(i, j) ) * *HaematocritLevel(i, j_minus) ;
			//cout<<*PassByMass(i, j)<<"ByMass"<<endl; 
	}
	
	if(*FlowY(i, j_plus)*(-1.0) > 0&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
			*Flow(i, j) += min_abs( *FlowY(i, j_plus) );
			*PassByMass(i, j) += min_abs( *FlowY(i, j_plus) ) * *HaematocritLevel(i, j_plus);
			//cout<<*PassByMass(i, j)<<"ByMass"<<endl; 
			
	}
		
	

	
	pCurrentVesselPoint = pCurrentVesselPoint -> pNextVesselPoint;
	}
	
	/**********
		ofstream myfile100;
	 myfile100.open ("PassByMass.txt");
  for( int iii=0 ; iii < Xnodes ; iii++ )
 {
  for( int jjj=0 ; jjj < Ynodes ; jjj++ )
  {
  myfile100 <<*PassByMass(iii,jjj)<<"   ";
  }
  myfile100<< endl<<endl;
}
myfile100<< endl<<endl;
  myfile100.close();
  ***********/

	
     
	//Compute Hematocrit
	pCurrentVesselPoint = pFirstVesselPoint;
	
	
	while(pCurrentVesselPoint)
	{
	     i = pCurrentVesselPoint->i;
		 j = pCurrentVesselPoint->j;
			
	if(pCurrentVesselPoint->AmIArtery)
	{
		//*HemX(i, j) =0.45;//+= min_abs(*FlowX(i, j) * AngioDeltaT 
		            //        / ( pi * pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength )
					//	    * ( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) ) / MaxHt) ;
		*HaematocritLevel(i, j) =0.45;//+= min_abs(*FlowY(i, j) * AngioDeltaT 
		              //      / ( pi * pow( *BloodVesselRadiusYdir(i, j), 2 ) * NodeLength )
						//    * ( TimeFactor * AverageHt - TimeFactor * *HemY(i, j) ) / MaxHt) ;	
							//cout<<*FlowX(i, j)<<"  "<<AngioDeltaT<<"  "<<pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength<<"  "<<( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) )<<endl;
							//cout<<*HemX(i, j)<<"   "<<*HemY(i, j)<<endl;
	}
	else
	{
		if(*Flow(i, j) > FlowMin)
		{		
		
				
		*HaematocritLevel(i, j) += min_abs(AngioDeltaT * TimeFactor/( pi * pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength ))/ MaxHt
							* ( *PassByMass(i, j)  -  *Flow(i, j)* *HaematocritLevel(i, j) )  ;
			
									//cout<<*HaematocritLevel(i, j)<<endl;							
	 
		
		}
	}
	
	if( *HaematocritLevel(i, j) > 1.0 )
	{
		*HaematocritLevel(i, j) = 1.0;
	}
	else if( *HaematocritLevel(i, j) < 0.0 )
	{
		*HaematocritLevel(i, j) = 0.0;
		//cout<<"Something is wrong! or not conserved"<<endl;
	}
	
	pCurrentVesselPoint = pCurrentVesselPoint -> pNextVesselPoint;
	}
	
	  
cout <<"function UpdateHematocrit finished"<<endl; 
}


/*
double VesselStructure::PTUpdate_Wave(Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& Variable,int i,int j)
{

 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix ConductX, ConductY;
 extern int Xnodes, Ynodes;

 double Output=0.0;
 double PassByMass=0;
 double Coeffecient=0;
 int i_minus, j_minus, i_plus, j_plus;

 

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if(*CoeffecientX(i, j)/ *ConductX(i,j) >0.001 &&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
			Coeffecient += min_abs( *CoeffecientX(i, j));

			PassByMass += min_abs( *CoeffecientX(i, j) ) * *Variable(i_minus, j) ;
			//cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if(*CoeffecientX(i_plus, j)*(-1.0)/ *ConductX(i,j) > 0.001&&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
			Coeffecient += min_abs( *CoeffecientX(i_plus, j) );
			PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * *Variable(i_plus, j) ;
			//cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if(*CoeffecientY(i, j)/ *ConductY(i,j) > 0.001&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			Coeffecient += min_abs( *CoeffecientY(i, j) );
			PassByMass += min_abs( *CoeffecientY(i, j) ) * *Variable(i, j_minus) ;
			//cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if(*CoeffecientY(i, j_plus)*(-1.0)/ *ConductY(i,j) > 0.001&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
			Coeffecient += min_abs( *CoeffecientY(i, j_plus) );
			PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * *Variable(i, j_plus);
			//cout<<PassByMass<<"ByMass"<<endl; 
			
	}
	
	if(Coeffecient>0)
	{
	Output=(PassByMass)/(Coeffecient);
	}
	else
	{
	Output=*Variable(i,j);
	}	
	 
		
	return Output;
}
*/
double VesselStructure::PTUpdate(Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& Variable,int i,int j)
{

 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix ConductX, ConductY;
 extern int Xnodes, Ynodes;

 double Output=0.0;
 double PassByMass=0;
 double Coeffecient=0;
 int i_minus, j_minus, i_plus, j_plus;

 

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
			Coeffecient += min_abs( *CoeffecientX(i, j));

			PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j) +*Variable(i, j) )/2;
			//cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
			Coeffecient += min_abs( *CoeffecientX(i_plus, j) );
			PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j) +*Variable(i, j) )/2;
			//cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			Coeffecient += min_abs( *CoeffecientY(i, j) );
			PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus) +*Variable(i, j) )/2;
			//cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
			Coeffecient += min_abs( *CoeffecientY(i, j_plus) );
			PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus) +*Variable(i, j) )/2;
			//cout<<PassByMass<<"ByMass"<<endl; 
			
	}
	
	if(Coeffecient>0)
	{
	Output=1.85*(PassByMass)/(Coeffecient) - 0.85* *Variable(i, j);
	}
	else
	{
	Output = 0;
	}
	 
		
	return Output;
}

void VesselStructure::SteadyStateHematocrit()
{
//cout <<"function UpdateHematocrit running"<<endl; 

 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodNodePressure;
 extern Matrix BloodVesselPressureX;
 extern Matrix BloodVesselIndicatorX,BloodVesselIndicatorY;
 extern Matrix FlowX, FlowY;
 extern Matrix WssX, WssY;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern int Xnodes, Ynodes;
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;

	extern bool VesselBroke;
    double FlowMin=1.0e-30;
	double TimeStepMax= AngioDeltaT;
	int MinTime_i,MinTime_j;
	double pi=3.141592654;
	double TimeStep = AngioDeltaT;
	double TimeFactor;
	TimeFactor = 1.0;
	double MaxHt = RetrieveDoubleParameter( ParameterFile , "MaxHt");
	double AverageHt = RetrieveDoubleParameter( ParameterFile , "AverageHt");
	Matrix Flow;
	Matrix PassByMass;
	Flow = zeros( Xnodes, Ynodes);
	PassByMass = zeros( Xnodes, Ynodes);
	int i_minus, j_minus, i_plus, j_plus;
	int i, j;
	double Tolerance=0.00001;
	int Iterations=0;
	double MaxError=2*Tolerance;
	int MaxIterations=5000000;
	
	while(pCurrentVesselPoint)
	{
		 i = pCurrentVesselPoint->i;
		 j = pCurrentVesselPoint->j;
		
		//cout<<"here after"<<endl;
				//g<<"here before"<<endl;
		
		//cout<<*Flow(i,j)<<endl;
		//cout<<min_abs( *FlowX(i, j) )<<endl;
		
	//	cout<<*FlowX(i,j)<<endl;
	//	cout<<*FlowY(i,j)<<endl;
		if(min_abs( *FlowX(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusXdir(i, j), 2) * NodeLength / min_abs( *FlowX(i, j) );
			//cout<<"TimeStep"<<TimeStep<<endl;
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
				//cout<<"TimeStepMax"<<TimeStepMax<<endl;
			}
			
		}
		
		if(min_abs( *FlowY(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusYdir(i, j), 2) * NodeLength / min_abs( *FlowY(i, j) );
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
			}

		}
	  
	  pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
	}
	
	//AngioDeltaT = 0.05;//TimeStepMax / TimeFactor;
	cout<<"AngioDeltaT"<<AngioDeltaT<<endl;
   //if(VesselBroke)//add on Sep 22,2009
  // {
  // HaematocritLevel=zeros(Xnodes, Ynodes);//add on Sep 13, 2009
  // VesselBroke=0;
  // }
while(MaxError>Tolerance&&Iterations<MaxIterations)
 {
//relocalize the point
 pCurrentVesselPoint = pFirstVesselPoint;
 MaxError=0;
 double Error=0.0;
  //sweep along the chain
 while(pCurrentVesselPoint)
 {
 i=pCurrentVesselPoint->i;
 j=pCurrentVesselPoint->j;
 if(pCurrentVesselPoint->AmIArtery||pCurrentVesselPoint->AmIVenous)
	{
		//*HemX(i, j) =0.45;//+= min_abs(*FlowX(i, j) * AngioDeltaT 
		            //        / ( pi * pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength )
					//	    * ( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) ) / MaxHt) ;
		*HaematocritLevel(i, j) =0.45;//+= min_abs(*FlowY(i, j) * AngioDeltaT 
		              //      / ( pi * pow( *BloodVesselRadiusYdir(i, j), 2 ) * NodeLength )
						//    * ( TimeFactor * AverageHt - TimeFactor * *HemY(i, j) ) / MaxHt) ;	
							//cout<<*FlowX(i, j)<<"  "<<AngioDeltaT<<"  "<<pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength<<"  "<<( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) )<<endl;
							//cout<<*HemX(i, j)<<"   "<<*HemY(i, j)<<endl;
	//cout<<"Venous or artery"<<i<<" ,"<<j<<"   ";
	//getchar();
	
	}
 // else if(i==0)
 // {
 //    *HaematocritLevel(i, j) =0.45;
  //}
  else
  {
   // cout<<"PTU ING"<<endl;
  double NewValue = PTUpdate(FlowX, FlowY, HaematocritLevel, i, j);
   // cout<<"PTU END"<<endl;
    Error = min_abs( NewValue - *HaematocritLevel(i,j) );
   if( Error > MaxError )
	{ MaxError = Error;
	//cout<<"at"<<i<<" ,"<<j<<"   "<<NewValue<<"   ";
	 }
	*HaematocritLevel(i,j) = NewValue;
 
//cout<<"NewValue:  "<<NewValue<<endl;


   
   }	 

 
  
   pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//Move on
  }
 //REVERSE THE DIRECTION
 //cout<<endl;

 pCurrentVesselPoint = pLastVesselPoint;
 
 while(pCurrentVesselPoint)
 {
 i=pCurrentVesselPoint->i;
 j=pCurrentVesselPoint->j;
 if(pCurrentVesselPoint->AmIArtery||pCurrentVesselPoint->AmIVenous)
	{
		//*HemX(i, j) =0.45;//+= min_abs(*FlowX(i, j) * AngioDeltaT 
		            //        / ( pi * pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength )
					//	    * ( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) ) / MaxHt) ;
		*HaematocritLevel(i, j) =0.45;//+= min_abs(*FlowY(i, j) * AngioDeltaT 
		              //      / ( pi * pow( *BloodVesselRadiusYdir(i, j), 2 ) * NodeLength )
						//    * ( TimeFactor * AverageHt - TimeFactor * *HemY(i, j) ) / MaxHt) ;	
							//cout<<*FlowX(i, j)<<"  "<<AngioDeltaT<<"  "<<pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength<<"  "<<( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) )<<endl;
							//cout<<*HemX(i, j)<<"   "<<*HemY(i, j)<<endl;
	}
//	else if(i==0)
 // {
 //    *HaematocritLevel(i, j) =0.45;
  //}
 else
   {
 double NewValue = PTUpdate(FlowX, FlowY, HaematocritLevel, i, j);
 
  Error = min_abs( NewValue - *HaematocritLevel(i,j) );
 if( Error > MaxError )
	{ MaxError = Error;
	//cout<<"at"<<i<<" ,"<<j<<"   "<<NewValue<<"   ";
	}
	*HaematocritLevel(i,j) = NewValue; 
	
	
  
   }
   
 pCurrentVesselPoint = pCurrentVesselPoint->pPreviousVesselPoint;//Move on
 }

 Iterations++;


 Iterations++;

  if( Iterations % 1000 == 0 )
  { cout << Iterations << " : " << MaxError << endl; }  

 }
 cout << "summary:: iterations: " << Iterations << endl
      << "          error     : " << MaxError << endl << endl; 
//AngioDeltaT=0.05;	  
  
 cout <<"function UpdateHematocrit finished"<<endl;
 

 }
/********************
void VesselStructure::UpdateHematocrit()
{

cout <<"function UpdateHematocrit running"<<endl; 

 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodNodePressure;
 extern Matrix BloodVesselPressureX;
// extern Matrix DirectionX, DirectionY;
 extern Matrix FlowX, FlowY;
 extern Matrix WssX, WssY;
 extern Matrix HemX, HemY;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern int Xnodes, Ynodes;
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;


    double FlowMin=1.0e-30;
	double TimeStepMax= AngioDeltaT;
	int MinTime_i,MinTime_j;
	double pi=3.141592654;
	double TimeStep = AngioDeltaT;
	double TimeFactor;
	TimeFactor = 1.0;
	double MaxHt = RetrieveDoubleParameter( ParameterFile , "MaxHt");
	double AverageHt = RetrieveDoubleParameter( ParameterFile , "AverageHt");
	Matrix Flow;
	Matrix PassByMass;
	Flow = zeros( Xnodes, Ynodes);
	PassByMass = zeros( Xnodes, Ynodes);
	int i_minus, j_minus, i_plus, j_plus;
	int i, j;
	while(pCurrentVesselPoint)
	{
		 i = pCurrentVesselPoint->i;
		 j = pCurrentVesselPoint->j;
		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = 0;
		}
		else
		{
			j_plus = j + 1;
		}
		
		if(i == Xnodes-1)// boundary condition again
		{
			i_plus = 0;
		}
		else
		{
			i_plus = i + 1;
		}
		
		
		//cout<<*Flow(i,j)<<endl;
		//cout<<min_abs( *FlowX(i, j) )<<endl;
		
	//	cout<<*FlowX(i,j)<<endl;
	//	cout<<*FlowY(i,j)<<endl;
		if(min_abs( *FlowX(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusXdir(i, j), 2) * NodeLength / min_abs( *FlowX(i, j) );
			//cout<<"TimeStep"<<TimeStep<<endl;
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
				//cout<<"TimeStepMax"<<TimeStepMax<<endl;
			}
			
		}
		
		if(min_abs( *FlowY(i, j) ) > FlowMin)
		{
			TimeStep = pi * pow( *BloodVesselRadiusYdir(i, j), 2) * NodeLength / min_abs( *FlowY(i, j) );
			if (TimeStep < TimeStepMax)
			{
				TimeStepMax = TimeStep;
			}

		}
	  
	  pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
	}
	
	AngioDeltaT = 0.005;//TimeStepMax / TimeFactor;
	//cout<<"AngioDeltaT"<<AngioDeltaT<<endl;
	
	pCurrentVesselPoint = pFirstVesselPoint;
	
	//get bypassmass
	while(pCurrentVesselPoint)
	{
		 i = pCurrentVesselPoint->i;
		 j = pCurrentVesselPoint->j;
		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = 0;
		}
		else
		{
			j_plus = j + 1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = 0;
		}
		else
		{
			i_plus = i + 1;
		}

	if(*FlowX(i, j) > 0)
	{
			*PassByMass(i, j) += min_abs( *FlowX(i, j) ) * *HemX(i, j) * AngioDeltaT * TimeFactor;
			//cout<<*PassByMass(i, j)<<"ByMass"<<endl; 
	}
	
	if(*FlowX(i_plus, j)*(-1.0) > 0)
	{
			*PassByMass(i, j) += min_abs( *FlowX(i_plus, j) ) * *HemX(i_plus, j) * AngioDeltaT * TimeFactor;
			//cout<<*PassByMass(i, j)<<"ByMass"<<endl; 
	}
	
	if(*FlowY(i, j) > 0)
	{
	
			*PassByMass(i, j) += min_abs( *FlowY(i, j) ) * *HemY(i, j) * AngioDeltaT * TimeFactor;
			//cout<<*PassByMass(i, j)<<"ByMass"<<endl; 
	}
	
	if(*FlowY(i, j_plus)*(-1.0) > 0)
	{
			*PassByMass(i, j) += min_abs( *FlowY(i, j_plus) ) * *HemY(i, j_plus) * AngioDeltaT * TimeFactor;
			//cout<<*PassByMass(i, j)<<"ByMass"<<endl; 
			
	}
		
	

	
	pCurrentVesselPoint = pCurrentVesselPoint -> pNextVesselPoint;
	}
	
		
     
	//Compute Hematocrit
	pCurrentVesselPoint = pFirstVesselPoint;
	
	
	while(pCurrentVesselPoint)
	{
	     i = pCurrentVesselPoint->i;
		 j = pCurrentVesselPoint->j;
			
	if(pCurrentVesselPoint->AmIArtery)
	{
		*HemX(i, j) =0.6;//+= min_abs(*FlowX(i, j) * AngioDeltaT 
		            //        / ( pi * pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength )
					//	    * ( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) ) / MaxHt) ;
		*HemY(i, j) =0.6;//+= min_abs(*FlowY(i, j) * AngioDeltaT 
		              //      / ( pi * pow( *BloodVesselRadiusYdir(i, j), 2 ) * NodeLength )
						//    * ( TimeFactor * AverageHt - TimeFactor * *HemY(i, j) ) / MaxHt) ;	
							//cout<<*FlowX(i, j)<<"  "<<AngioDeltaT<<"  "<<pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength<<"  "<<( TimeFactor * AverageHt - TimeFactor * *HemX(i, j) )<<endl;
							//cout<<*HemX(i, j)<<"   "<<*HemY(i, j)<<endl;
	}
	else
	{
		if(*Flow(i, j) >  0)
		{		
		
		if(*FlowX(i, j) > 0)
		{
		
		*HemX(i, j) += min_abs(*FlowX(i, j) * AngioDeltaT 
		                    / ( pi * pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength )
							* ( *PassByMass(i-1, j) / *Flow(i, j) / AngioDeltaT - TimeFactor * *HemX(i, j) ) / MaxHt );
							//cout<<*HemX(i, j)<<endl;
		
		}
		else if(*FlowX(i, j) < 0)

		{
		*HemX(i, j) += min_abs(*FlowX(i, j) * AngioDeltaT 
		                    / ( pi * pow( *BloodVesselRadiusXdir(i, j), 2 ) * NodeLength )
							* ( *PassByMass(i, j) / *Flow(i, j) / AngioDeltaT - TimeFactor * *HemX(i, j) ) / MaxHt );
		}
		
		if(*FlowY(i, j) < 0)
		{
		
		
		*HemY(i, j) += min_abs(*FlowY(i, j) * AngioDeltaT 
		                    / ( pi * pow( *BloodVesselRadiusYdir(i, j), 2 ) * NodeLength )
						    * ( *PassByMass(i, j) / *Flow(i, j) / AngioDeltaT - TimeFactor * *HemY(i, j) ) / MaxHt );
							//cout<<*HemY(i, j)<<endl;	
							//cout<<*HemX(i, j)<<" notsprout  "<<*HemY(i, j)<<endl;
							
	    }
		
		else if(*FlowY(i, j) > 0)
		{
		*HemY(i, j) += min_abs(*FlowY(i, j) * AngioDeltaT 
		                    / ( pi * pow( *BloodVesselRadiusYdir(i, j), 2 ) * NodeLength )
						    * ( *PassByMass(i, j-1) / *Flow(i, j) / AngioDeltaT - TimeFactor * *HemY(i, j) ) / MaxHt );
		}
		
		}
	}
	
	if( *HemX(i, j) > 1.0 )
	{
		*HemX(i, j) = 1.0;
	}
	else if( *HemX(i, j) < 0.0 )
	{
		cout<<"Something is wrong! or not conserved"<<endl;
	}
	
	if( *HemY(i, j) > 1.0 )
	{
		*HemY(i, j) = 1.0;
	}
	else if( *HemY(i, j) < 0.0 )
	{
		cout<<"Something is wrong! or not conserved"<<endl;
	}	
	pCurrentVesselPoint = pCurrentVesselPoint -> pNextVesselPoint;
	}
	

//cout <<"function UpdateHematocrit finished"<<endl; 
}
*************/

void VesselStructure::UpdateViscosity()
{
 //cout <<"function UpdateViscosity running"<<endl; 
 //extern Matrix DirectionX, DirectionY;
 extern Matrix FlowX, FlowY;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
 //extern Matrix HemX, HemY;
 extern Matrix HaematocritLevel;
 extern Matrix WssX, WssY;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix ConductX, ConductY; //conductivity
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 double mu_plasma = RetrieveDoubleParameter( ParameterFile, "mu_plasma");
 double AverageHt = RetrieveDoubleParameter( ParameterFile , "AverageHt");
 double Temp1, Temp2, TempHt, Temp_045;
 extern int Xnodes, Ynodes;
  ofstream myfile15;
  myfile15.open ("debugVis.txt");




 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 int i, j;
 while(pCurrentVesselPoint)
 {
	i = pCurrentVesselPoint->i;
	j = pCurrentVesselPoint->j;
	//if(!(pCurrentVesselPoint->AmIArtery))
	{
	
	
		//this may change, how do they come?
		if(i!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
		{
		
		if( (*HaematocritLevel(i-1, j)+*HaematocritLevel(i, j))/2 < 0.0001 )
	{
		*VisX(i, j) = mu_plasma;
		//cout<<*VisX(i,j);
	}
	
	else
		{
		Temp1 = 1.0 / ( 1 + pow( 10.0, -11.0 ) * pow( 2.0 * *BloodVesselRadiusXdir(i, j), 12.0 ) );
		Temp2 = (0.8 + exp( -0.15 * *BloodVesselRadiusXdir(i, j) ) ) * ( -1.0 + Temp1 ) + Temp1;
		TempHt = ( pow( (1.0 - (*HaematocritLevel(i-1, j)+*HaematocritLevel(i, j))/2 ), Temp2 ) - 1.0 ) / ( pow( ( 1.0 - AverageHt ), Temp2 ) - 1.0 ); 
		Temp_045 = 6 * exp( -0.17 * *BloodVesselRadiusXdir(i, j) )
		           + 3.2 - 2.44 * exp( -0.06 * pow(2 * *BloodVesselRadiusXdir(i, j), 0.645) );  
		*VisX(i, j) = mu_plasma * pow( ( 2 * *BloodVesselRadiusXdir(i, j)*1e6 / ( 2 *  *BloodVesselRadiusXdir(i, j)*1e6 - 1.1 ) ), 2 )
					 *( 1 + ( Temp_045 - 1 ) * TempHt * pow( ( 2 * *BloodVesselRadiusXdir(i, j)*1e6 / ( 2 *  *BloodVesselRadiusXdir(i, j)*1e6 - 1.1 ) ), 2 ) );
		
		}
		//myfile15 <<"VisX "<<*VisX(i,j)<<" at  "<<i<<", "<<j<<endl;
		}			 
		
	
	
	if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
		{
		
		if( (*HaematocritLevel(i, j-1)+*HaematocritLevel(i, j))/2 < 0.0001 )
	{
		*VisY(i, j) = mu_plasma;
	}
	else
	   {
		Temp1 = 1.0 / ( 1 + pow( 10.0, -11.0 ) * pow( 2.0 * *BloodVesselRadiusYdir(i, j), 12.0 ) );
		Temp2 = (0.8 + exp( -0.15 * *BloodVesselRadiusYdir(i, j) ) ) * ( -1.0 + Temp1 ) + Temp1;
		TempHt = ( pow( (1.0 - (*HaematocritLevel(i, j-1)+*HaematocritLevel(i, j))/2 ), Temp2 ) - 1.0 ) / ( pow( ( 1.0 - AverageHt ), Temp2 ) - 1.0 ); 
		Temp_045 = 6 * exp( -0.17 * *BloodVesselRadiusYdir(i, j) )
		           + 3.2 - 2.44 * exp( -0.06 * pow(2 * *BloodVesselRadiusYdir(i, j), 0.645) );  
		*VisY(i, j) = mu_plasma * pow( ( 2 * *BloodVesselRadiusYdir(i, j)*1e6 / ( 2 *  *BloodVesselRadiusYdir(i, j)*1e6 - 1.1 ) ), 2 )
					 *( 1 + ( Temp_045 - 1 ) * TempHt * pow( ( 2 * *BloodVesselRadiusYdir(i, j)*1e6 / ( 2 *  *BloodVesselRadiusYdir(i, j) *1e6- 1.1 ) ), 2 ) );
		
		}
		//myfile15 <<"VisY "<<*VisY(i,j)<<" at  "<<i<<", "<<j<<endl;
		}
	
	}
	
	pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;

 }
 
 
	cout <<"function UpdateViscosity finished"<<endl; 
myfile15.close();

}


void VesselStructure::UpdateWallShearStress()
{

//cout <<"function UpdateWallShearStress running"<<endl; 

 extern Matrix WssX, WssY;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix ConductX, ConductY;
 extern Matrix FlowX, FlowY;
 extern int Xnodes, Ynodes;
 extern Matrix TracerX, TracerY;
 double PI = 3.141592654;
 double P_a = RetrieveDoubleParameter( ParameterFile, "P_a");
 double P_b = RetrieveDoubleParameter( ParameterFile, "P_b");
 double P_r = RetrieveDoubleParameter( ParameterFile, "P_c");
 double P_d = RetrieveDoubleParameter( ParameterFile, "P_d");
 MaxWss = 0;
 
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 int i, j;
 ofstream myfile14;
  myfile14.open ("debug2.txt");
  WssX = zeros(Xnodes, Ynodes);
  WssY = zeros(Xnodes, Ynodes);
  TracerX = zeros(Xnodes,Ynodes);
  TracerY = zeros(Xnodes,Ynodes);
 while( pCurrentVesselPoint)
 {
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;

	//if(min_abs(*FlowX(i,j))<1e-15)
	//{*WssX(i, j) = 0.0;}
	//else
	{
	//if(j==200)
	//{
	if(i!=0&&(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5))
	{
	*WssX(i, j) = *BloodVesselRadiusXdir(i, j)/(2*NodeLength)* min_abs(*FlowX(i,j))/ *ConductX(i,j);//4.0 * *VisX(i, j) * min_abs( *FlowX(i, j) ) / ( PI * pow( *BloodVesselRadiusXdir(i, j), 3.0 ) ) ;
	*TracerX(i, j) = 1e18*pow(*BloodVesselRadiusXdir(i,j),3)*P_a* pow(P_d,2) *exp(-P_b*(1+P_r*pow(P_d,0.5))* *WssX(i,j));
	}
	//}
	//else
	//{
	//*WssX(i, j) =15.0;
	//}
	}
	//if(min_abs(*FlowY(i,j))<1e-15)
	//{*WssY(i,j) = 0.0;}
	//else
	{
	//if(j==200)
	//{
	if(j!=0&&(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5))
	{
	*WssY(i, j) =*BloodVesselRadiusYdir(i, j)/(2*NodeLength)* min_abs(*FlowY(i,j))/ *ConductY(i,j);//4.0 * *VisY(i, j) * min_abs( *FlowY(i, j) ) / ( PI * pow( *BloodVesselRadiusYdir(i, j), 3.0 ) ) ;
	*TracerY(i, j) = 1e18*pow(*BloodVesselRadiusYdir(i,j),3)*P_a* pow(P_d,2) *exp(-P_b*(1+P_r*pow(P_d,0.5))* *WssY(i,j));
	}
	//}
	//else
	//{
	//*WssY(i, j)=15.0;
	//}
	}
	//cout<<"WssX"<<*WssX(i,j)<<endl;
	if(*WssX(i, j) > MaxWss)
	{
		MaxWss = *WssX(i, j);
		//myfile14 <<"MaxWssX"<<MaxWss<<"   "<<i<<", "<<j<<" "<<"RadiusX"<<*BloodVesselRadiusXdir(i, j)<<"NodeLength"<<NodeLength<<"Flow"<<*FlowX(i,j)<<"CondX"<<*ConductX(i,j)<<endl;
	}
	
	if(*WssY(i, j) > MaxWss)
	{
		MaxWss = *WssY(i, j);
		//myfile14 <<"MaxWssY"<<MaxWss<<"   "<<i<<", "<<j<<" "<<"RadiusY"<<*BloodVesselRadiusYdir(i, j)<<"NodeLength"<<NodeLength<<"Flow"<<*FlowY(i,j)<<"CondY"<<*ConductY(i,j)<<endl;

	}
	
	pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 }
	
myfile14.close();

//cout <<"function UpdateWallShearStress finished"<<endl;
}

void VesselStructure::UpdateRadius_R()
{
cout <<"function UpdateRadius running"<<endl;
 //--------- TauE calculated here is in Pascal ----------------

	//-----------------------------------IMPORTANT --------------------------------------
	// TauE(P) this law is given for P in mmHg and TauE in dyn/cm2
	// TauE is multiplied by 0.1 in order to convert dyn/cm2 into Pascal (1dyn/cm2=0.1Pa)
	// The pressures (NodalPressures) are multiplied by 0.007501 in order to convert 
	// Pascal into mmHg (1Pa=0.007501mmHg)
		double convPa=0.007501;
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 extern double t;
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
 //extern Matrix DirectionX, DirectionY;
 
 extern Matrix BloodVesselPressureX, BloodVesselPressureY; //This is contained in older code which I don't know it's neccesity. But anyway...
 //Also the older one " CalculatePressureInVessel" is geometrically BLIND.
 Matrix TauEX, TauEY;
 TauEX = zeros( Xnodes, Ynodes);
 TauEY = zeros( Xnodes, Ynodes);
    double Sm, Swss, Sp;
    double coeffks;
	double coeffkp;  
	double coeffkm; 
	coeffks = RetrieveDoubleParameter( ParameterFile , "coeffks");
    coeffkp = RetrieveDoubleParameter( ParameterFile , "coeffkp");
    coeffkm = RetrieveDoubleParameter( ParameterFile , "coeffkm");

	double TauRef=0.103; //(dyn/cm2)
	double FlowRef=1.9096e-11; //m3/s (Flow in PV)
	   double RadiusLimitInf=RetrieveDoubleParameter( ParameterFile , "RadiusLimitInf" ); 
	double RadiusLimitSup=RetrieveDoubleParameter( ParameterFile , "RadiusLimitSup" );
	double RadiusVariation;
    int i=0;
	int j=0;
	int v=0;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 //cout<<pFirstVesselPoint->i<<"  "<<pFirstVesselPoint->j<<endl;
 while( pCurrentVesselPoint)
 {
 
	if((pCurrentVesselPoint->AmIArtery)==0)
	
{
	i = pCurrentVesselPoint-> i;
	
	j = pCurrentVesselPoint-> j;
	//cout<<i<<"  "<<j<<endl;
	v = pCurrentVesselPoint-> MyAge;

	if(v>=VesselAgeInf)
	{


  if(i!=0&&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))
  {

  

   
	 if(convPa * *BloodVesselPressureX(i, j) < 10.0)//for small pressure
 {
	*BloodVesselPressureX(i, j) = 10.1 / convPa;
  }
 *TauEX(i, j) = 0.1 * ( 100.0 - 86.0 * exp( -5000 * pow( log10( log10( convPa * *BloodVesselPressureX(i, j) ) ), 5.4 ) ) ) ;//why is here 0.1??


 if(*FlowX(i,j)==0||(*HaematocritLevel(i-1, j)+*HaematocritLevel(i, j))/2<0.001)
 {
 Sm = 0;//coeffkm * log10( ( FlowRef / ( min_abs( *FlowX(i, j) ) * *HemX(i, j) ) ) + 1.0 );
 }
 else
 {
 Sm = coeffkm * log10( ( FlowRef / ( min_abs( *FlowX(i, j) ) * (*HaematocritLevel(i-1, j)+*HaematocritLevel(i, j))/2 ) ) + 1.0 );
 }
 
  Sp = coeffkp * log10( 10.0 * *TauEX(i, j) );
 
 Swss = log10(  *WssX(i, j)*10 + TauRef );

 RadiusVariation = *BloodVesselRadiusXdir(i, j) * AngioDeltaT * ( Swss - Sp + Sm - coeffks );// Adaptionmodify
 //cout<<"RadiusVariation"<<RadiusVariation;
 *BloodVesselRadiusXdir(i, j) += RadiusVariation;

 //cout<<"BVI"<<*BloodVesselRadiusXdir(i, j);

if (*BloodVesselRadiusXdir(i, j) < RadiusLimitInf )
 {
   *BloodVesselRadiusXdir(i, j) = RadiusLimitInf;
 }
//todday
if (*BloodVesselRadiusXdir(i, j) > RadiusLimitSup )
 {
   *BloodVesselRadiusXdir(i, j) = RadiusLimitSup;
 }

 
 }
// Y direction


  if(j!=0&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
 {

//getchar();
  if(convPa * *BloodVesselPressureY(i, j) < 10.0)
 {
	*BloodVesselPressureY(i, j) = 10.1 / convPa;
 }
 
 //getchar();
 *TauEY(i, j) = 0.1 * ( 100.0 - 86.0 * exp( -5000 * pow( log10( log10( convPa * *BloodVesselPressureY(i, j) ) ), 5.4 ) ) ) ;//why is here 0.1??
 if(*FlowY(i,j)==0||(*HaematocritLevel(i, j-1)+*HaematocritLevel(i, j))/2<0.001)
 {
 Sm = 0.0;//coeffkm * log10( ( FlowRef / ( min_abs( *FlowY(i, j) ) * *HemY(i, j) ) ) + 1.0 );
 }
 else
 {
 Sm = coeffkm * log10( ( FlowRef / ( min_abs( *FlowY(i, j) ) * (*HaematocritLevel(i, j-1)+*HaematocritLevel(i, j))/2 ) ) + 1.0 );
 }
 Sp = coeffkp * log10( 10.0 * *TauEY(i, j) );
// cout<<"SpY"<<Sp;
 Swss = log10( *WssY(i,j)*10 + TauRef );
// cout<<"WssY"<<*WssY(i,j)<<endl;
 // cout<<"Swss"<<Swss<<endl<<"Sm"<<Sm<<endl<<"Sp"<<Sp<<endl<<"coeffks"<<coeffks<<endl<<endl;
 RadiusVariation = *BloodVesselRadiusYdir(i, j) * AngioDeltaT * ( Swss - Sp + Sm - coeffks );// Adaptionmodify
 *BloodVesselRadiusYdir(i, j) += RadiusVariation;
 //cout<<i<<"   "<<j<<" (*BloodVesselIndicatorY(i,j)  "<<*BloodVesselIndicatorY(i,j)<<"  "<<RadiusVariation<<"   "<<*BloodVesselRadiusYdir(i, j);


 if (*BloodVesselRadiusYdir(i, j) < RadiusLimitInf )
  {
   *BloodVesselRadiusYdir(i, j) = RadiusLimitInf;
  }

 if (*BloodVesselRadiusYdir(i, j) > RadiusLimitSup )
{
  *BloodVesselRadiusYdir(i, j) = RadiusLimitSup;
}
 

}




 
  }
//  cout<<*BloodVesselRadiusXdir(i, j)<<"   "<< *BloodVesselRadiusYdir(i, j)<<"  ";
//  getchar();
	
 }
  

pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
 cout <<"function UpdateRadius finished"<<endl;
//getchar();
}

void VesselStructure::UpdateRadius()
{
cout <<"function UpdateRadius running"<<endl;
 //--------- TauE calculated here is in Pascal ----------------

	//-----------------------------------IMPORTANT --------------------------------------
	// TauE(P) this law is given for P in mmHg and TauE in dyn/cm2
	// TauE is multiplied by 0.1 in order to convert dyn/cm2 into Pascal (1dyn/cm2=0.1Pa)
	// The pressures (NodalPressures) are multiplied by 0.007501 in order to convert 
	// Pascal into mmHg (1Pa=0.007501mmHg)
		double convPa=0.007501;
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
 //extern Matrix DirectionX, DirectionY;
 
 extern Matrix BloodVesselPressureX, BloodVesselPressureY; //This is contained in older code which I don't know it's neccesity. But anyway...
 //Also the older one " CalculatePressureInVessel" is geometrically BLIND.
 Matrix TauEX, TauEY;
 extern Matrix Stability;
 TauEX = zeros( Xnodes, Ynodes);
 TauEY = zeros( Xnodes, Ynodes);
    double Sm, Swss, Sp;
    double coeffks;
	double coeffkp;  
	double coeffkm; 
	coeffks = RetrieveDoubleParameter( ParameterFile , "coeffks");
    coeffkp = RetrieveDoubleParameter( ParameterFile , "coeffkp");
    coeffkm = RetrieveDoubleParameter( ParameterFile , "coeffkm");

	double TauRef=0.103; //(dyn/cm2)
	double FlowRef=1.4e-13;//1.0e-11;//FlowRef=3.3e-13;//3.3e-12;//1.9096e-11; //m3/s (Flow in PV)

	   double RadiusLimitInf=RetrieveDoubleParameter( ParameterFile , "RadiusLimitInf" ); 
	double RadiusLimitSup=RetrieveDoubleParameter( ParameterFile , "RadiusLimitSup" );
	double RadiusVariation;
    int i=0;
	int j=0;
	int v=0;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 //cout<<pFirstVesselPoint->i<<"  "<<pFirstVesselPoint->j<<endl;
 while( pCurrentVesselPoint)
 {
 
	if((pCurrentVesselPoint->AmIArtery)==0)
	
{
	i = pCurrentVesselPoint-> i;
	
	j = pCurrentVesselPoint-> j;
	//cout<<i<<"  "<<j<<endl;
	double RandNumber = float(rand())/float(RAND_MAX);

	if(RandNumber<*Stability(i,j))
	{


  if(i!=0&&min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5)
  {

  

   
	 if(convPa * *BloodVesselPressureX(i, j) < 10.0)//for small pressure
  {
	*BloodVesselPressureX(i, j) = 10.1 / convPa;
  }
 *TauEX(i, j) = 0.1 * ( 100.0 - 86.0 * exp( -5000 * pow( log10( log10( convPa * *BloodVesselPressureX(i, j) ) ), 5.4 ) ) ) ;//why is here 0.1??


 if(*FlowX(i,j)==0||(*HaematocritLevel(i-1, j)+*HaematocritLevel(i, j))/2<0.001)
 {
 Sm = 0;//coeffkm * log10( ( FlowRef / ( min_abs( *FlowX(i, j) ) * *HemX(i, j) ) ) + 1.0 );
 }
 else
 {
 Sm = coeffkm * log10( ( FlowRef / ( min_abs( *FlowX(i, j) ) * (*HaematocritLevel(i-1, j)+*HaematocritLevel(i, j))/2 ) ) + 1.0 );
 }
 
// cout<<"Sm"<<Sm;
 Sp = coeffkp * log10(  *TauEX(i, j) );
// cout<<"SpX"<<Sp;
 Swss = log10(  *WssX(i, j)*10 + TauRef );
// cout<<"SwssX"<<Swss;
 //getchar();
 
 RadiusVariation = *BloodVesselRadiusXdir(i, j) * AngioDeltaT * ( Swss - Sp + Sm - coeffks );// Adaptionmodify
 //cout<<"RadiusVariation"<<RadiusVariation;
 *BloodVesselRadiusXdir(i, j) += RadiusVariation;
 //cout<<"BVI"<<*BloodVesselRadiusXdir(i, j);

if (*BloodVesselRadiusXdir(i, j) < RadiusLimitInf )
 {
   *BloodVesselRadiusXdir(i, j) = RadiusLimitInf;
 }
//todday
if (*BloodVesselRadiusXdir(i, j) > RadiusLimitSup )
 {
   *BloodVesselRadiusXdir(i, j) = RadiusLimitSup;
 }

 
 }
// Y direction


  if(j!=0&&min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5)
 {

//getchar();
  if(convPa * *BloodVesselPressureY(i, j) < 10.0)
 {
	*BloodVesselPressureY(i, j) = 10.1 / convPa;
 }
 
 //getchar();
 *TauEY(i, j) = 0.1 * ( 100.0 - 86.0 * exp( -5000 * pow( log10( log10( convPa * *BloodVesselPressureY(i, j) ) ), 5.4 ) ) ) ;//why is here 0.1??
 if(*FlowY(i,j)==0||(*HaematocritLevel(i, j-1)+*HaematocritLevel(i, j))/2<0.001)
 {
 Sm = 0.0;//coeffkm * log10( ( FlowRef / ( min_abs( *FlowY(i, j) ) * *HemY(i, j) ) ) + 1.0 );
 }
 else
 {
 Sm = coeffkm * log10( ( FlowRef / ( min_abs( *FlowY(i, j) ) * (*HaematocritLevel(i, j-1)+*HaematocritLevel(i, j))/2 ) ) + 1.0 );
 }
 Sp = coeffkp * log10(  *TauEY(i, j) );
 //cout<<"SpY"<<Sp;
 Swss = log10( *WssY(i,j)*10 + TauRef );
 //cout<<"WssY"<<*WssY(i,j)<<endl;
 // cout<<"Swss"<<Swss<<endl<<"Sm"<<Sm<<endl<<"Sp"<<Sp<<endl<<"coeffks"<<coeffks<<endl<<endl;
 RadiusVariation = *BloodVesselRadiusYdir(i, j) * AngioDeltaT * ( Swss - Sp + Sm - coeffks );// Adaptionmodify
 *BloodVesselRadiusYdir(i, j) += RadiusVariation;


 if (*BloodVesselRadiusYdir(i, j) < RadiusLimitInf )
  {
   *BloodVesselRadiusYdir(i, j) = RadiusLimitInf;
  }

 if (*BloodVesselRadiusYdir(i, j) > RadiusLimitSup )
{
  *BloodVesselRadiusYdir(i, j) = RadiusLimitSup;
}
 

}




 
  }
//  cout<<*BloodVesselRadiusXdir(i, j)<<"   "<< *BloodVesselRadiusYdir(i, j)<<"  ";
//  getchar();
	
 }
  

pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
 cout <<"function UpdateRadius finished"<<endl;

}


void VesselStructure::UpdateRadius_Cell()
{
cout <<"function UpdateRadius_cell running"<<endl;
 //--------- TauE calculated here is in Pascal ----------------

	//-----------------------------------IMPORTANT --------------------------------------
	// TauE(P) this law is given for P in mmHg and TauE in dyn/cm2
	// TauE is multiplied by 0.1 in order to convert dyn/cm2 into Pascal (1dyn/cm2=0.1Pa)
	// The pressures (NodalPressures) are multiplied by 0.007501 in order to convert 
	// Pascal into mmHg (1Pa=0.007501mmHg)
	
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix SproutTips;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadius;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY, BloodVesselIndicator;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 extern Matrix TAF;
 extern Matrix VesselCells;
 
 double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");

  double min_C = RetrieveDoubleParameter( ParameterFile , "min_C");
 //extern Matrix DirectionX, DirectionY;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 //Matrix PressureAverageX, PressureAverageY; //This is contained in older code which I don't know it's neccesity. But anyway...

	double RadiusLimitInf=RetrieveDoubleParameter( ParameterFile , "RadiusLimitInf" ); 
	double RadiusLimitSup=RetrieveDoubleParameter( ParameterFile , "RadiusLimitSup" );
	double Rmin=RetrieveDoubleParameter( ParameterFile , "Rmin" );
    double r;
    int i, j;
 //BloodVesselRadiusXdir = zeros(Xnodes,Ynodes);
 //BloodVesselRadiusYdir = zeros(Xnodes,Ynodes);
 //BloodVesselRadius = zeros(Xnodes,Ynodes);
 
 
  while( pCurrentVesselPoint)
 {
 
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;
r =0;
 if(pCurrentVesselPoint-> MyAge < VesselAgeInf)
{
 if(*BloodVesselIndicator(i,j)>0.5)
 {
 if(*VesselCells(i,j)>0)
 {
 r = min_C* *VesselCells(i,j)/(*BloodVesselIndicator(i,j));
 }
 cout<<"r"<<r;
 if ( r>= Rmin)
 {
   *BloodVesselRadius(i, j) = r;
   if(*SproutTips(i,j)==0)
   {
   pCurrentVesselPoint-> MyAge = VesselAgeInf;
   }
 }
 else
 {
 *BloodVesselRadius(i, j) = r;
  pCurrentVesselPoint-> MyAge = 0;
 }
 
 //cout<<"BVI"<<*BloodVesselRadiusXdir(i, j);
 }
//if (*BloodVesselRadius(i, j) < RadiusLimitInf )
// {
//   *BloodVesselRadius(i, j) = 0;
// }
//todday

}



pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }//update point by point
 
 
 
 
 
 
 pCurrentVesselPoint = pFirstVesselPoint;

 
 while( pCurrentVesselPoint)
 {
 
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;
 if(pCurrentVesselPoint-> MyAge < VesselAgeInf)
 {

  if(i!=0&&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))
  {
  
  
  *BloodVesselRadiusXdir(i, j) = (*BloodVesselRadius(i-1, j)+*BloodVesselRadius(i, j))/2;
  
  
  
  
  
   if (*BloodVesselRadiusXdir(i, j) < RadiusLimitInf )
 {
   *BloodVesselRadiusXdir(i, j) = 0;
 }
//todday
if (*BloodVesselRadiusXdir(i, j) > RadiusLimitSup )
 {
   *BloodVesselRadiusXdir(i, j) = RadiusLimitSup;
 }
  }//if connected X
  
  if(i!=Xnodes-1&&(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
  {
  
  
  *BloodVesselRadiusXdir(i+1, j) = (*BloodVesselRadius(i, j)+*BloodVesselRadius(i+1, j))/2;
  
  
  
  
  
   if (*BloodVesselRadiusXdir(i+1, j) < RadiusLimitInf )
 {
   *BloodVesselRadiusXdir(i+1, j) = 0;
 }
//todday
if (*BloodVesselRadiusXdir(i+1, j) > RadiusLimitSup )
 {
   *BloodVesselRadiusXdir(i+1, j) = RadiusLimitSup;
 }
  }//if conn

  if(j!=0&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
  {




*BloodVesselRadiusYdir(i, j) = (*BloodVesselRadius(i, j-1)+*BloodVesselRadius(i, j))/2;


if (*BloodVesselRadiusYdir(i, j) < RadiusLimitInf )
  {
   *BloodVesselRadiusYdir(i, j) = 0;
  }

 if (*BloodVesselRadiusYdir(i, j) > RadiusLimitSup )
{
  *BloodVesselRadiusYdir(i, j) = RadiusLimitSup;
}
 
  }//if connected Y
  
  if(j!=Ynodes-1&&(min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
  {




*BloodVesselRadiusYdir(i, j+1) = (*BloodVesselRadius(i, j)+*BloodVesselRadius(i, j+1))/2;


if (*BloodVesselRadiusYdir(i, j+1) < RadiusLimitInf )
  {
   *BloodVesselRadiusYdir(i, j+1) = 0;
  }

 if (*BloodVesselRadiusYdir(i, j+1) > RadiusLimitSup )
{
  *BloodVesselRadiusYdir(i, j+1) = RadiusLimitSup;
}
 
  }//if connected Y

  
 
 }
pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
  cout <<"function UpdateRadius_cell finished"<<endl;

}


 void VesselStructure:: UpdateConductivity()
{
	//cout <<"function UpdateConductivity running"<<endl;
	extern Matrix ConductX, ConductY;
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix VisX, VisY;
	extern int Xnodes,Ynodes;
	double pi = 3.141592654;
	double MaxConduct= 0;
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;

	
	while(pCurrentVesselPoint)
	{
		int i = pCurrentVesselPoint->i;
		int j = pCurrentVesselPoint->j;
		if(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*ConductX(i, j) = pi * pow( *BloodVesselRadiusXdir(i, j), 4 ) / ( 8.0 * *VisX(i, j) * NodeLength );
		if(*ConductX(i, j)>MaxConduct)
	    {MaxConduct=*ConductX(i, j);
		//cout<<" i  "<<i<<"  j  "<<j<<" MaxConduct "<<MaxConduct<<endl;
		//getchar();
		}
		}
		if(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*ConductY(i, j) = pi * pow( *BloodVesselRadiusYdir(i, j), 4 ) / ( 8.0 * *VisY(i, j) * NodeLength );
		
		if(*ConductY(i, j)>MaxConduct)
	    {MaxConduct=*ConductY(i, j);
		//cout<<" i  "<<i<<"  j  "<<j<<" MaxConduct "<<MaxConduct<<endl;
		//getchar();

		}
		}
		//cout<<*ConductX(i,j)<<endl;
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
			
	 }
	 /***********Normalize************/
	 
	 
	 
	/* 
	 pCurrentVesselPoint = pFirstVesselPoint;
	 
	 while(pCurrentVesselPoint)
	{
		int i = pCurrentVesselPoint->i;
		int j = pCurrentVesselPoint->j;
		if(*BloodVesselIndicatorX(i,j)<-0.5||min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*ConductX(i, j) = *ConductX(i, j)/MaxConduct;
		
		}
		if(*BloodVesselIndicatorY(i,j)<-0.5||min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*ConductY(i, j) = *ConductY(i, j)/MaxConduct;

		
		}
		//cout<<*ConductX(i,j)<<endl;
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
			
	 }
	 */
   

	cout <<"function UpdateConductivity finished"<<endl;
}
/**********
void VesselStructure:: GetHaematocritValue()
{

//cout <<"function GetHaematocritValue running"<<endl;
extern Matrix HaematocritLevel;
extern Matrix HemX;
extern Matrix HemY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;

extern int Xnodes, Ynodes;
HaematocritLevel = zeros(Xnodes, Ynodes);
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{

*HaematocritLevel(pCurrentVesselPoint->i,pCurrentVesselPoint->j) = 0;
*HaematocritLevel(pCurrentVesselPoint->i,pCurrentVesselPoint->j) 
+=*HemX(pCurrentVesselPoint->i,pCurrentVesselPoint->j);
if(pCurrentVesselPoint->i + 1 < Xnodes - 1)
{ 
*HaematocritLevel(pCurrentVesselPoint->i,pCurrentVesselPoint->j) 
+=*HemX(pCurrentVesselPoint->i + 1,pCurrentVesselPoint->j);
}
*HaematocritLevel(pCurrentVesselPoint->i,pCurrentVesselPoint->j) 
+=*HemY(pCurrentVesselPoint->i,pCurrentVesselPoint->j);

if(pCurrentVesselPoint->j + 1 < Ynodes - 1)
{
*HaematocritLevel(pCurrentVesselPoint->i,pCurrentVesselPoint->j) 
+=*HemY(pCurrentVesselPoint->i,pCurrentVesselPoint->j + 1);
}

*HaematocritLevel(pCurrentVesselPoint->i,pCurrentVesselPoint->j) 
/=min_abs(*BloodVesselIndicatorX(pCurrentVesselPoint->i,pCurrentVesselPoint->j))+min_abs(*BloodVesselIndicatorY(pCurrentVesselPoint->i,pCurrentVesselPoint->j));
//cout<<*HaematocritLevel(pCurrentVesselPoint->i,pCurrentVesselPoint->j) <<endl;

pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}

	     


//cout <<"function GetHaematocritValue finished"<<endl;


}

*************/


void VesselStructure::WSSTAFBranchByProbInterval()// We can develop others by physiological observation too,WAIT UNTIL 3 D,,,,THERE ARE SO MANY PROBLEM!!OR...I INVENT SOME CRITERIA
{
	//cout <<"function WSSTAFBranchByProbInterval running"<<endl;

extern Matrix WssX, WssY;
double SproutAgeThreshold = RetrieveDoubleParameter( ParameterFile , "SproutAgeThreshold");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
double VesselAgeSup = RetrieveDoubleParameter( ParameterFile , "VesselAgeSup");
extern int Xnodes, Ynodes;
extern Matrix TAF;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;

if( MaxWss < 1e-5 )
	{ MaxWss = 2.0; }
double RandNumber;
double ProbBranch;
int RandBranchDirection;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint;
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint;
 
 ofstream myfile13;
  myfile13.open ("debug.txt");
 
  

 

while ( pCurrentVesselPoint1 )
{
 int i = pCurrentVesselPoint1-> i;
 int j = pCurrentVesselPoint1-> j;
 /*  cout << "else if: " << (i>1) << ", " << i << endl;
  cout << "else if: " << (i<Xnodes-2) << endl;
  cout << "else if: " << (*SproutTips(i, j) < 1.0e-5) << ", " << *SproutTips(i,j) << endl;
  cout << "else if: " << (*VesselAge(i, j) > VesselAgeInf) << ", " << *VesselAge(i,j) << ", " << VesselAgeInf << endl;
  cout << "else if: " << (*VesselAge(i, j) < VesselAgeSup) << ", " << VesselAgeSup << endl;
  cout << "if: " << (*SproutAge(i, j) > SproutAgeThreshold) << ", " << *SproutAge(i,j) << ", " << SproutAgeThreshold << endl;
 */
   if( *SproutTips(i, j) > 1e-5 && *SproutAge(i, j) > SproutAgeThreshold&&1==0)
  {
		//cout << "I am wrong" << endl;
        if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.2;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.3;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.4;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 1.0;
		}
		
		 RandNumber = float(rand())/float(RAND_MAX);
         RandBranchDirection = 1+int(2*float(rand())/float(RAND_MAX));
		if( RandNumber < ProbBranch )
		{
		  int Grow = 0;
		if( *BloodVesselIndicatorX(i, j) < 0)
		   {
				int I = i + 1;
				int J = j;
		  if( (*BloodVesselIndicator(I, J)) < 10e-16)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
			if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

		    
			    }
			
		
		if( RandBranchDirection == 1)
		{
		  I = i;
		  J = j + 1;
		   if(*BloodVesselIndicator(I, J)< 10e-16)
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionY(I, J) = 1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));		
		}  
		}
		else if ( RandBranchDirection == 2)
		{
		  I = i;
		  J = j - 1;
		if( *BloodVesselIndicator(I,J)< 10e-16)
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = -1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
			}
		
		  }
		
		}
		else if( *BloodVesselIndicatorX(i, j) > 0)
		{
		int I = i - 1;
		int J = j;
		
		  if( *BloodVesselIndicator(I, J) < 10e-16)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		    //*DirectionX(I, J) = -1;
				if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		//}  
		if( RandBranchDirection == 1)
		{
		  I = i;
		  J = j - 1;
		if( *BloodVesselIndicator(I, J) < 10e-16)
		{   Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		 //   *SproutAge(i, j) = 0;
		  //  *DirectionY(I, J) = -1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

		} 
		}
		else if ( RandBranchDirection == 2)
		{
		  I = i;
		  J = j + 1;
		if( *BloodVesselIndicator(I, J)< 10e-16)
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = 1;

		 		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));		} 
		
		}
		}
		
		else if( *BloodVesselIndicatorY(i, j) < 0)
		   {
				int I = i ;
				int J = j + 1;
		   if( *BloodVesselIndicator(I, J) < 10e-16)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = 1;
		  		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));			    }
	
		
		if( RandBranchDirection == 1)
		{
		  I = i + 1;
		  J = j ;
          if( *BloodVesselIndicator(I, J) < 10e-16)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
		  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		}
 
		else if ( RandBranchDirection == 2)
		{
		  I = i - 1;
		  J = j ;
		 if( *BloodVesselIndicator(I, J) < 10e-16)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionX(I, J) = -1;
		  		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));			    }
		
		    }
		}
		
		else if( *BloodVesselIndicatorY(i, j) > 0)
		{
		int I = i ;
		int J = j - 1;
		  if( *BloodVesselIndicator(I, J) < 10e-16)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		    //*DirectionY(I, J) = -1;
	if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		

		//}  
		if( RandBranchDirection == 1)
		{
		  I = i - 1;
		  J = j;
	    if(*BloodVesselIndicator(I, J)< 10e-16)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		  //  *DirectionX(I, J) = -1;
		   		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J)); 
			    }

		}  
		else if ( RandBranchDirection == 2)
		{
		  I = i + 1;
		  J = j;
		 if( *BloodVesselIndicator(I, J)< 10e-16)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
		 if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

			    }
		}
  
  }//DIRECTIONY END
  
  if(Grow > 1)
  {
  SproutNumber++;
  }
  
  
  }//IF PROB END
  
  
  }//SPROUT END
 
  //else if( (i>1)&&(i<Xnodes-2)&&(j>1)&&(j<200)&&*SproutTips(i, j) < 1e-5 && *VesselAge(i, j) > VesselAgeInf && *VesselAge(i, j) < VesselAgeSup)

  else if( (i>1)&&(i<Xnodes-2)&&*SproutTips(i, j) < 1e-5 && *VesselAge(i, j) > VesselAgeInf && *VesselAge(i, j) < VesselAgeSup)
  {
	if( *WssX(i, j) <= 0.2 * MaxWss )
	{
	ProbBranch = 0.0;
	}
	
	else if ( *WssX(i, j) > 0.2 * MaxWss && *WssX(i, j) <= 0.4 * MaxWss )
	{
	    if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.02;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.03;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.1;
		}
	}
	
	else if ( *WssX(i, j) > 0.4 * MaxWss && *WssX(i, j) <= 0.6 * MaxWss )
    {
		if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.2;
		}
	}
	
	else if ( *WssX(i, j) > 0.6 * MaxWss && *WssX(i, j) <= 0.8 * MaxWss )
    {
		if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.09;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.3;
		}
	}
  
	else if ( *WssX(i, j) > 0.8 * MaxWss )
	{
		if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.16;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.4;
		}
	} 
	
	
	// X direction
	     RandNumber = float(rand())/float(RAND_MAX);
        RandBranchDirection = 1+int(2*float(rand())/float(RAND_MAX));
		
		//if(j==Ynodes-1)
		//{
		//cout << "********* 2 **********" << endl;
		//getchar();
		//myfile13 <<MaxWss<<" MaxWss "<<*WssX(i,j)<<" Wss "<<MaxTAF<<" MaxTAF "<<*TAF(i,j)<<" TAF "<<ProbBranch<<" ProbBranch "<<RandNumber<<" RandNumber "<<RandBranchDirection<<" RandBranchDirection "<<endl;
		//}
	
	if(RandNumber < ProbBranch && min_abs(*BloodVesselIndicatorY(i, j))<1.5 )
	{
	 int Grow = 0;
	if(RandBranchDirection == 1&&j<(Ynodes-1))
	   {
	      int I = i;
		  int J = j + 1;
		   if(min_abs(*BloodVesselIndicator(I, J))<10e-5)
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionY(I, J) = 1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		      {
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		      }
		else
			{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
			    {*BloodVesselIndicatorY(i,j)=2;}
		else
			    {*BloodVesselIndicatorY(i,j)=1;}
			}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
				{
		         *BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
				}
		else
				{
		if(*BloodVesselIndicatorY(I,J)>0.5)
				{*BloodVesselIndicatorY(I,J)=2;}
		else
				{*BloodVesselIndicatorY(I,J)=-1;}
				}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));	
			}	
	
	
		}
	else if( RandBranchDirection == 2&&j>0 )
	{
	int I = i;
	int J = j - 1;
	
	if( *BloodVesselIndicator(I,J)< 10e-16)
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = -1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
		}
		
	  }
	if(Grow > 0)
  {
  SproutNumber++;
  }

	
   }
	
	
	
		 
	
	// Y Direction
	
	
	if( *WssY(i, j) <= 0.1 * MaxWss )//0.2
	{
	ProbBranch = 0.0;
	}
	
	else if ( *WssY(i, j) > 0.1 * MaxWss && *WssY(i, j) <= 0.2 * MaxWss )//0.2 0.4
	{
	    if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.02;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.03;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.1;
		}
	}
	
	else if ( *WssY(i, j) > 0.2 * MaxWss && *WssY(i, j) <= 0.4 * MaxWss )//0.4 0.6
    {
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.2;
		}
	}
	
	else if ( *WssY(i, j) > 0.4 * MaxWss && *WssY(i, j) <= 0.6 * MaxWss )//0.6 0.8
    {
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.09;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.3;
		}
	}
  
	else if ( *WssY(i, j) > 0.6 * MaxWss )//0.8
	{
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.16;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.4;
		}
	} 
	
	     RandNumber = float(rand())/float(RAND_MAX);
         RandBranchDirection = 1+int(2*float(rand())/float(RAND_MAX));
		 
		  if(RandNumber < ProbBranch && min_abs(*BloodVesselIndicatorX(i, j))<1.5)
		 {
		 int Grow = 0;
		 if(RandBranchDirection == 1 )
		 {
		 int I = i + 1;
		 int J = j;
		
		if( min_abs(*BloodVesselIndicator(I, J))< 10e-16)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
		  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		}
		 else if( RandBranchDirection == 2)
		 {
		 int I = i - 1;
		 int J = j;
      if( min_abs(*BloodVesselIndicator(I, J)) < 10e-16)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionX(I, J) = -1;
		  		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));			   
		 }
		
		}
	if(Grow > 0)
  {
  SproutNumber++;
  }
	}


  }


 
pCurrentVesselPoint1 = pCurrentVesselPoint1 -> pNextVesselPoint;

}
 myfile13.close();

pLastVesselPoint = pCurrentVesselPoint2;
//cout <<"function WSSTAFBranchByProbInterval finished"<<endl;
//extern Matrix DirectionY;

}


void VesselStructure::TransOutPutThresholdFlow()
{
int i, j, i_minus, j_minus, i_plus, j_plus;
extern int Xnodes, Ynodes;
extern Matrix BloodVesselIndicator, BloodVesselIndicatorX, BloodVesselIndicatorY;
extern Matrix Flow, FlowX, FlowY;
extern Matrix Vis, VisX, VisY;
extern Matrix BloodVesselRadius, BloodVesselRadiusXdir, BloodVesselRadiusYdir;
Flow = zeros(Xnodes,Ynodes);
Vis = zeros(Xnodes,Ynodes);
BloodVesselRadius = zeros(Xnodes,Ynodes);
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
i=pCurrentVesselPoint->i;
j=pCurrentVesselPoint->j;



		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
			if(*FlowX(i, j) >1e-15)
			{
			*Flow(i,j) += min_abs( *FlowX(i, j));
			}
			*Vis(i,j) += *VisX(i,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i,j);
			
						
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		if(*FlowX(i_plus, j)*(-1.0) > 1e-15)
			{
			*Flow(i,j) += min_abs( *FlowX(i_plus, j) );
			}
			*Vis(i,j) += *VisX(i_plus,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i_plus,j);
						
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			if(*FlowY(i, j) > 1e-15)
			{
			*Flow(i,j) += min_abs( *FlowY(i, j) );
			}
			
			*Vis(i,j) += *VisY(i,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j);
	
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
			if(*FlowY(i, j_plus)*(-1.0) > 1e-15)
			{
			*Flow(i,j) += min_abs( *FlowY(i, j_plus) );
		//cout<<" Flow "<<*Flow(i,j);
			}
			*Vis(i,j) += *VisY(i,j_plus);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j_plus);
						
	}
	
	*Vis(i,j) /= *BloodVesselIndicator(i,j);
	*BloodVesselRadius(i,j) /= *BloodVesselIndicator(i,j);
 // cout<<"BVR"<<*BloodVesselRadius(i,j);




pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}

 //SetBoundary flow to 0
	 


}


void VesselStructure::TransOutPut()
{
int i, j, i_minus, j_minus, i_plus, j_plus;
extern int Xnodes, Ynodes;
extern Matrix BloodVesselIndicator, BloodVesselIndicatorX, BloodVesselIndicatorY;
extern Matrix Flow, FlowX, FlowY;
extern Matrix Vis, VisX, VisY;
extern Matrix WssX, WssY;
extern Matrix Wss;
extern Matrix MapIndicator, MapRadius, MapFlow, MapPressure, MapVesselCells, MapSproutTips, Tracer,TracerX,TracerY, TracerS, Wss;
extern Matrix BloodVesselRadius, BloodVesselRadiusXdir, BloodVesselRadiusYdir;
Flow = zeros(Xnodes,Ynodes);
Vis = zeros(Xnodes,Ynodes);
BloodVesselRadius = zeros(Xnodes,Ynodes);
Tracer = zeros(Xnodes, Ynodes);
TracerS = zeros(Xnodes, Ynodes);
Wss = zeros(Xnodes, Ynodes);

extern Matrix BloodNodePressure, BloodVesselPressureX, BloodVesselPressureY;
extern Matrix SproutTips, VesselCells;
double pi = 3.141592654;
MapIndicator = zeros(2*Xnodes-1, 2*Ynodes-1);
MapVesselCells = zeros(2*Xnodes-1, 2*Ynodes-1);
MapSproutTips = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapFlow = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapRadius = zeros(2*Xnodes-1, 2*Ynodes-1);
	MapPressure = zeros(2*Xnodes-1, 2*Ynodes-1);

VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
i=pCurrentVesselPoint->i;
j=pCurrentVesselPoint->j;

if(pCurrentVesselPoint->AmIArtery)
{
	if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
	
			
			
			*Flow(i,j) += min_abs( *FlowX(i, j));
			*Vis(i,j) += *VisX(i,j);
			*Wss(i,j) += *WssX(i,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i,j);
			*Tracer(i,j) += *TracerX(i,j);
			
						
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
			*Flow(i,j) += min_abs( *FlowX(i_plus, j) );
			
			*Vis(i,j) += *VisX(i_plus,j);
			*Wss(i,j) += *WssX(i_plus,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i_plus,j);
			*Tracer(i,j) += *TracerX(i_plus,j);
						
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			
			*Flow(i,j) += min_abs( *FlowY(i, j) );
			
			
			*Vis(i,j) += *VisY(i,j);
			*Wss(i,j) += *WssY(i,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j);
			*Tracer(i,j) += *TracerY(i,j);
	
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
			*Flow(i,j) += min_abs( *FlowY(i, j_plus) );
		//cout<<" Flow "<<*Flow(i,j);
			
			*Vis(i,j) += *VisY(i,j_plus);
			*Wss(i,j) += *WssY(i,j_plus);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j_plus);
			*Tracer(i,j) += *TracerY(i,j_plus);
						
	}
	
	if(*BloodVesselIndicator(i,j)>0)
	{
	*Vis(i,j) /= *BloodVesselIndicator(i,j);
	*BloodVesselRadius(i,j) /= *BloodVesselIndicator(i,j);
	*Wss(i,j) /= *BloodVesselIndicator(i,j);

	*Tracer(i,j) /= *BloodVesselIndicator(i,j);
	*TracerS(i,j) = pi* *BloodVesselRadius(i,j)* *Tracer(i,j);
	}
 // cout<<"BVR"<<*BloodVesselRadius(i,j);

}
else
{

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
	
			
			if(*FlowX(i, j) >0)
			{
			*Flow(i,j) += min_abs( *FlowX(i, j));
			}
			*Vis(i,j) += *VisX(i,j);
			*Wss(i,j) += *WssX(i,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i,j);
			*Tracer(i,j) += *TracerX(i,j);
			
						
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		if(*FlowX(i_plus, j)*(-1.0) > 0)
			{
			*Flow(i,j) += min_abs( *FlowX(i_plus, j) );
			}
			*Vis(i,j) += *VisX(i_plus,j);
			*Wss(i,j) += *WssX(i_plus,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusXdir(i_plus,j);
			*Tracer(i,j) += *TracerX(i_plus,j);
						
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			if(*FlowY(i, j) > 0)
			{
			*Flow(i,j) += min_abs( *FlowY(i, j) );
			}
			
			*Vis(i,j) += *VisY(i,j);
			*Wss(i,j) += *WssY(i,j);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j);
			*Tracer(i,j) += *TracerY(i,j);
	
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
			if(*FlowY(i, j_plus)*(-1.0) > 0)
			{
			*Flow(i,j) += min_abs( *FlowY(i, j_plus) );
		//cout<<" Flow "<<*Flow(i,j);
			}
			*Vis(i,j) += *VisY(i,j_plus);
			*Wss(i,j) += *WssY(i,j_plus);
			*BloodVesselRadius(i,j) += *BloodVesselRadiusYdir(i,j_plus);
			*Tracer(i,j) += *TracerY(i,j_plus);
						
	}
	
	if(*BloodVesselIndicator(i,j)>0)
	{
	*Vis(i,j) /= *BloodVesselIndicator(i,j);
	*BloodVesselRadius(i,j) /= *BloodVesselIndicator(i,j);
	*Wss(i,j) /= *BloodVesselIndicator(i,j);

	*Tracer(i,j) /= *BloodVesselIndicator(i,j);
	*TracerS(i,j) = pi* *BloodVesselRadius(i,j)* *Tracer(i,j);
	}
 // cout<<"BVR"<<*BloodVesselRadius(i,j);

}


pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}

 //SetBoundary flow to 0
 for(int ii =0; ii< 2*Xnodes-1 ; ii++)
{
 
 for(int jj =0; jj< 2*Ynodes-1 ; jj++)
 {
 
 if(ii%2&&jj%2)
 {
if(min_abs(*BloodVesselIndicator(ii/2,jj/2))>0.5)
 {
 *MapIndicator(ii,jj) = 1;
 }	
 *MapRadius(ii,jj) = *BloodVesselRadius(ii/2,jj/2);
 *MapFlow(ii,jj) =*Flow(ii/2,jj/2);
 *MapPressure(ii,jj) = *BloodNodePressure(ii/2,jj/2);
 *MapVesselCells(ii,jj) = *VesselCells(ii/2,jj/2);
 *MapSproutTips(ii,jj) = *SproutTips(ii/2,jj/2);
 
 }
 else if(ii%2&&(!(jj%2)))
 {
 if(min_abs(*BloodVesselIndicatorY(ii/2,(jj+1)/2))>1.5||*BloodVesselIndicatorY(ii/2,(jj+1)/2)<-0.5)
 {
 *MapIndicator(ii,jj) = 1;
 }
 
 *MapRadius(ii,jj) = *BloodVesselRadiusYdir(ii/2,(jj+1)/2);
 *MapFlow(ii,jj) = min_abs(*FlowY(ii/2,(jj+1)/2));
 *MapPressure(ii,jj) = *BloodVesselPressureY(ii/2,(jj+1)/2);
 }
 else if((!(ii%2))&&jj%2)
 {
 if(min_abs(*BloodVesselIndicatorX( (ii+1)/2,(jj)/2 ))>1.5||*BloodVesselIndicatorX( (ii+1)/2,(jj)/2 )<-0.5)
 {
 *MapIndicator(ii,jj) = 1;
 }
 
*MapRadius(ii,jj) = *BloodVesselRadiusXdir((ii+1)/2,(jj)/2);
 *MapFlow(ii,jj) = min_abs(*FlowX((ii+1)/2,(jj)/2));
*MapPressure(ii,jj) = *BloodVesselPressureX((ii+1)/2,(jj)/2);
 }
 
 }
 
 
 }
	 
	 
	 
	 


}



 void VesselStructure::RegenerateFlow()
{

extern Matrix Flow;
extern Matrix HaematocritLevel;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern double t;
VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
int i,j;
if(t<0.1)
{
FlowMax = 0.0;


while(pCurrentVesselPoint)
{
i=pCurrentVesselPoint->i;
j=pCurrentVesselPoint->j;

if(*Flow(i,j)>FlowMax )
{
FlowMax = *Flow(i,j);
}


pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}

}
cout<<"FlowMax"<<FlowMax<<endl;



pCurrentVesselPoint = pFirstVesselPoint;
while(pCurrentVesselPoint)
{
i=pCurrentVesselPoint->i;
j=pCurrentVesselPoint->j;

*Flow(i,j)= *Flow(i,j)/FlowMax;	
	//if (*Flow(i,j)>0.1)
	//{
	 // cout << *Flow(i,j)<<endl;
	//}
	
		
pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
}

 //SetBoundary flow to 0
 


}

/***********


void VesselStructure::Kill(Matrix& Pressure)
{

//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix SproutTips;
extern Matrix VesselAge;
extern Matrix SproutAge;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix ECM;
extern int Xnodes, Ynodes;

int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
int LoopLength = VesselListLength; 

}
***********/
void VesselStructure::InterAngioPressureUpdate(int MaxIterations, double Tolerance)
 {
 extern Matrix ConductX,ConductY;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
 extern Matrix BloodNodePressure;
 extern Matrix LymIndicator;
 extern Matrix InterPressure;
 extern Matrix Permeability;
 extern Matrix Conduct;
 extern Matrix X,Y;
 extern int Xnodes, Ynodes;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;//initialize the updating point
 double Error=2*Tolerance;
 double MaxError=2*Tolerance;
 int Iterations=0;
 double NewValue=0.0;
 int i=0;
 int j=0;
double Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
double Pout = RetrieveDoubleParameter( ParameterFile , "Pout");
extern double collapseConstant;
collapseConstant = RetrieveDoubleParameter( ParameterFile , "collapseConstant");


Matrix DiffusionConstant = ones(Xnodes,Ynodes);



  double MaxConduct= 1e-50;
  
 for(int ii=0; ii<X.TellCols(); ii++)
 {
	for(int jj=0; jj<Y.TellCols(); jj++)
	{
	//cout<<*BloodVesselIndicatorX(ii,jj)<<endl;
	if(*ConductX(ii,jj)>MaxConduct)
	{MaxConduct=*ConductX(ii,jj);}
	if(*ConductY(ii,jj)>MaxConduct)
	{MaxConduct=*ConductY(ii,jj);}
	}
 }
 
  for(int ii=0; ii<X.TellCols(); ii++)
 {
	for(int jj=0; jj<Y.TellCols(); jj++)
	{
	//cout<<*BloodVesselIndicatorX(ii,jj)<<endl;
	*DiffusionConstant(ii,jj) = 0.01 * MaxConduct;
	}
 }
//BloodNodePressure=zeros(Xnodes,Ynodes);		
 
 while(MaxError>Tolerance&&Iterations<MaxIterations)
 {
//relocalize the point
 pCurrentVesselPoint = pFirstVesselPoint;
 MaxError=0;
  //sweep along the chain
 while(pCurrentVesselPoint)
 {
 i=pCurrentVesselPoint->i;
 j=pCurrentVesselPoint->j;

 if(pCurrentVesselPoint->AmIArtery)
 {
 if(j==0)
 {
 NewValue = Pin;
 }
 else if(j==50)
 {
 NewValue = Pin-250;
 }
 else if(j==150)
 {
 NewValue = Pin-500;
 }
  else if(j==200)
 {
 NewValue = Pin-750;
 }
 
 }
 else if(pCurrentVesselPoint->AmIVenous)
 {
 if(j==0)
 {
 NewValue = Pout+300;
 }
 else if(j==50)
 {
 NewValue = Pout+200;
 }
 else if(j==150)
 {
 NewValue = Pout+100;
 }
  else if(j==200)
 {
 NewValue = Pout;
 }

 }


 else
 {
 NewValue =AngioLymSORUpdate( Permeability, InterPressure, BloodNodePressure, ConductX, ConductY, BloodVesselIndicatorX, BloodVesselIndicatorY, i, j, MaxConduct);

 }	 
 
 Error = min_abs( NewValue - *BloodNodePressure(i,j) );
 if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 
 *BloodNodePressure(i,j) = NewValue;
 //update fluid
 
if(i==Xnodes-1||i==0||j==Ynodes-1||j==0)
{
NewValue = FluidPressureUpdateBoundaryPointD( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);

}
else
{
 NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
}
  Error = min_abs( NewValue - *InterPressure(i,j) );
  if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;
 
 pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//Move on
// cout << "          error     : " << MaxError <<  endl; 
 }
 
 //update fluid
 
 
 //boundary
  for( i=0 ; i <= Xnodes-1 ; i++ )
  {
   j = 0;
   NewValue = FluidPressureUpdateBoundaryPointD( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);
   Error = min_abs( NewValue - *InterPressure(i,j) );
   if( Error > MaxError )
	{ MaxError = Error; }
 *InterPressure(i, j) = NewValue;
   
   j = Ynodes-1;
  NewValue = FluidPressureUpdateBoundaryPointD( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);
   Error = min_abs( NewValue - *InterPressure(i,j) );
   if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;

  }
 
  for( j=1 ; j < Ynodes-1 ; j++ )
  {
   i = 0;
   NewValue = FluidPressureUpdateBoundaryPointD( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);
   Error = min_abs( NewValue - *InterPressure(i,j) );
   if( Error > MaxError )
	{ 
			MaxError = Error;
			
	}
 *InterPressure(i, j) = NewValue;

   i = Xnodes-1;
  NewValue = FluidPressureUpdateBoundaryPointD( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);
   Error = min_abs( NewValue - *InterPressure(i,j) );
   if( Error > MaxError )
	{ 
			MaxError = Error;
			
	}

 *InterPressure(i, j) = NewValue;
  }

 
 //unboundary
 if( Iterations % 4 == 0 )
  {
   for( j=1 ; j < Ynodes-1 ; j++ )
   {
    for( i=1 ; i < Xnodes-1 ; i++ )
    {
	
 NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
  Error = min_abs( NewValue - *InterPressure(i,j) );
 if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;

    }
   }
  }
  
  if( Iterations % 4 == 1 )
  {
   for( j=Ynodes-2 ; j > 0 ; j-- )
   {
    for( i=1 ; i < Xnodes-1 ; i++ )
    {
      NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
  Error = min_abs( NewValue - *InterPressure(i,j) );
if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;

    }
   }
  }
  
  if( Iterations % 4 == 2 )
  {
   for( j=Ynodes-2 ; j > 0 ; j-- )
   {
    for( i=Xnodes-2 ; i > 0 ; i-- )
    {
      NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
  Error = min_abs( NewValue - *InterPressure(i,j) );
 if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;
    }
   }
  }
  
  if( Iterations % 4 == 3 )
  {
   for( j=1 ; j < Ynodes-1 ; j++ )
   {
    for( i=Xnodes-2 ; i > 0 ; i-- )
    {
      NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
  Error = min_abs( NewValue - *InterPressure(i,j) );
 if( Error > MaxError )
	{ 
			MaxError = Error;
	
	}
 *InterPressure(i, j) = NewValue;

    }
   }
  }

 
 
 
 
 
 
 
 
 
 
 Iterations++;
//cout << "iterations: " << Iterations << endl
//	<< "          error     : " << MaxError <<  endl; 

  if( Iterations % 1000 == 0 )
  { cout << Iterations << " : " << MaxError << endl; }  
 }
 cout << "summary:: iterations: " << Iterations << endl
      << "          error     : " << MaxError << endl << endl; 
 
 
 }

 
void VesselStructure::InterAngioPressureUpdate(int MaxIterations, double Tolerance , double MaxConduct)
 {
 extern Matrix ConductX,ConductY;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY;
 extern Matrix BloodNodePressure;
 extern Matrix LymIndicator;
 extern Matrix InterPressure;
 extern Matrix Permeability;
 extern Matrix Conduct;
 extern Matrix X,Y;
 extern double t;
 extern int Xnodes, Ynodes;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;//initialize the updating point
 double Error=2*Tolerance;
 double MaxError=2*Tolerance;
 int Iterations=0;
 double NewValue=0.0;
 int i=0;
 int j=0;
double Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
double Pout = RetrieveDoubleParameter( ParameterFile , "Pout");
extern double collapseConstant;
collapseConstant = RetrieveDoubleParameter( ParameterFile , "collapseConstant");


Matrix DiffusionConstant = ones(Xnodes,Ynodes);




  for(int ii=0; ii<X.TellCols(); ii++)
 {
	for(int jj=0; jj<Y.TellCols(); jj++)
	{
	//cout<<*BloodVesselIndicatorX(ii,jj)<<endl;
	*DiffusionConstant(ii,jj) = 0.01 * MaxConduct;
	}
 }
//if(t<0.1)
//{
//BloodNodePressure=zeros(Xnodes,Ynodes);		
//}
 while(MaxError>Tolerance&&Iterations<MaxIterations)
 {
//relocalize the point
 pCurrentVesselPoint = pFirstVesselPoint;
 MaxError=0;
  //sweep along the chain
 while(pCurrentVesselPoint)
 {
 i=pCurrentVesselPoint->i;
 j=pCurrentVesselPoint->j;

 if(pCurrentVesselPoint->AmIArtery)
 {
 NewValue = Pin;
 //cout<<"Come here Pin";
 //getchar();
 }
 else if(pCurrentVesselPoint->AmIVenous)
 {
 NewValue = Pout;
 // cout<<"Come here Pout";
 //getchar();
 }


 else
 {
 NewValue =AngioLymSORUpdate( Permeability, InterPressure, BloodNodePressure, ConductX, ConductY, BloodVesselIndicatorX, BloodVesselIndicatorY, i, j, MaxConduct);
if(!(min_abs(NewValue)<100000000))
{cout<<*BloodVesselIndicatorX(i,j)<<"   "<<*BloodVesselIndicatorY(i,j)<<"   "<<i<<"  ,  "<<j<<"    "<<NewValue<<"   ";
//getchar();
 }	  
 }
 Error = min_abs( NewValue - *BloodNodePressure(i,j) );
 if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 
 *BloodNodePressure(i,j) = NewValue;
 //update fluid
 
if(i==Xnodes-1||i==0||j==Ynodes-1||j==0)
{
NewValue = FluidPressureUpdateBoundaryPointD( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);

}
else
{
 NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
}
  Error = min_abs( NewValue - *InterPressure(i,j) );
  if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;
 
 pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;//Move on
// cout << "          error     : " << MaxError <<  endl; 
 }
 
 //update fluid
 
 
 //boundary
  for( i=0 ; i <= Xnodes-1 ; i++ )
  {
   j = 0;
   NewValue = FluidPressureUpdateBoundaryPointD( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);
   Error = min_abs( NewValue - *InterPressure(i,j) );
   if( Error > MaxError )
	{ MaxError = Error; }
 *InterPressure(i, j) = NewValue;
   
   j = Ynodes-1;
  NewValue = FluidPressureUpdateBoundaryPointD( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);
   Error = min_abs( NewValue - *InterPressure(i,j) );
   if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;

  }
 
  for( j=1 ; j < Ynodes-1 ; j++ )
  {
   i = 0;
   NewValue = FluidPressureUpdateBoundaryPointD( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);
   Error = min_abs( NewValue - *InterPressure(i,j) );
   if( Error > MaxError )
	{ 
			MaxError = Error;
			
	}
 *InterPressure(i, j) = NewValue;

   i = Xnodes-1;
  NewValue = FluidPressureUpdateBoundaryPointD( DiffusionConstant,  BloodNodePressure, InterPressure, i, j);
   Error = min_abs( NewValue - *InterPressure(i,j) );
   if( Error > MaxError )
	{ 
			MaxError = Error;
			
	}

 *InterPressure(i, j) = NewValue;
  }

 
 //unboundary
 if( Iterations % 4 == 0 )
  {
   for( j=1 ; j < Ynodes-1 ; j++ )
   {
    for( i=1 ; i < Xnodes-1 ; i++ )
    {
	
 NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
  Error = min_abs( NewValue - *InterPressure(i,j) );
 if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;

    }
   }
  }
  
  if( Iterations % 4 == 1 )
  {
   for( j=Ynodes-2 ; j > 0 ; j-- )
   {
    for( i=1 ; i < Xnodes-1 ; i++ )
    {
      NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
  Error = min_abs( NewValue - *InterPressure(i,j) );
if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;

    }
   }
  }
  
  if( Iterations % 4 == 2 )
  {
   for( j=Ynodes-2 ; j > 0 ; j-- )
   {
    for( i=Xnodes-2 ; i > 0 ; i-- )
    {
      NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
  Error = min_abs( NewValue - *InterPressure(i,j) );
 if( Error > MaxError )
	{ 
			MaxError = Error;
	
		
	}
 *InterPressure(i, j) = NewValue;
    }
   }
  }
  
  if( Iterations % 4 == 3 )
  {
   for( j=1 ; j < Ynodes-1 ; j++ )
   {
    for( i=Xnodes-2 ; i > 0 ; i-- )
    {
      NewValue = AngioFluidPressureUpdate( DiffusionConstant,  BloodNodePressure, InterPressure,   i, j);
  Error = min_abs( NewValue - *InterPressure(i,j) );
 if( Error > MaxError )
	{ 
			MaxError = Error;
	
	}
 *InterPressure(i, j) = NewValue;

    }
   }
  }

 
 
 
 
 
 
 
 
 
 
 Iterations++;
//cout << "iterations: " << Iterations << endl
//	<< "          error     : " << MaxError <<  endl; 

  if( Iterations % 1000 == 0 )
  { cout << Iterations << " : " << MaxError << endl; }  
  
 }
 cout << "summary:: iterations: " << Iterations << endl
      << "          error     : " << MaxError << endl << endl; 
 
 

 
}

//double AngioLymSORUpdate( Matrix& Permeability, Matrix& InterPressure, Matrix& Variable, Matrix& CoeffecientX, Matrix& CoeffecientY, Matrix& BloodVesselIndicatorX, Matrix& BloodVesselIndicatorY,  int i, int j, double MaxConduct)
double AngioLymSORUpdate( Matrix& Permeability, Matrix& InterPressure, Matrix& Solution, Matrix& ConductX, Matrix& ConductY, Matrix& BloodVesselIndicatorX, Matrix& BloodVesselIndicatorY,  int i, int j, double MaxConduct)
{

 extern Matrix Conduct;
 extern Matrix BloodVesselRadius;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicator;
 double dt = 0.8/MaxConduct;
 extern double t;
 extern Matrix X;
 double OutPut = 0.0;
 extern int Xnodes,Ynodes;
// extern Matrix DirectionX,DirectionY;
 double p= *Permeability(i,j);

 double r= *BloodVesselRadius(i,j);

 

 /*
  double Output=0.0;
 double PassByMass=0;
 double Coeffecient=0;
 int i_minus, j_minus, i_plus, j_plus;

 

		if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
			Coeffecient += min_abs( *CoeffecientX(i, j));

			PassByMass += min_abs( *CoeffecientX(i, j) ) * (*Variable(i_minus, j) +*Variable(i, j) )/2;
			
			*Conduct(i,j)+= *CoeffecientX(i, j);
			//cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
			Coeffecient += min_abs( *CoeffecientX(i_plus, j) );
			PassByMass += min_abs( *CoeffecientX(i_plus, j) ) * (*Variable(i_plus, j) +*Variable(i, j) )/2;
			*Conduct(i,j)+= *CoeffecientX(i_plus, j);
		 //  cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
			Coeffecient += min_abs( *CoeffecientY(i, j) );
			PassByMass += min_abs( *CoeffecientY(i, j) ) * (*Variable(i, j_minus) +*Variable(i, j) )/2;
			*Conduct(i,j)+= *CoeffecientY(i, j);
			//cout<<PassByMass<<"ByMass"<<endl; 
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
			Coeffecient += min_abs( *CoeffecientY(i, j_plus) );
			PassByMass += min_abs( *CoeffecientY(i, j_plus) ) * (*Variable(i, j_plus) +*Variable(i, j) )/2;
			*Conduct(i,j)+= *CoeffecientY(i, j_plus);
		//	cout<<PassByMass<<"ByMass"<<endl; 
			
	}
	
	
	// *Conduct(i,j) = *Conduct(i,j) / *BloodVesselIndicator(i,j);
	// Coeffecient += *Conduct(i,j)* p;
	// PassByMass += *Conduct(i,j)* p * (*Variable(i, j)+ *InterPressure(i, j))/2;
	 
	
	if(Coeffecient>0)
	{
	Output=1.85*(PassByMass)/(Coeffecient) - 0.85* *Variable(i, j);
	//cout<<"Come here"<<Coeffecient<<"  "<<PassByMass<<"  ";
	//getchar();
	}
	else
	{
	Output = 0;
	}
	 
		
	return Output;

 */
 
 
 extern Matrix BloodVesselIndicatorX;
 extern Matrix BloodVesselIndicatorY;//So, BloodVessel is also important.

 
 
 double Uij = *Solution(i,j);
 double Ui_jp1 = 0;
 double Ui_jm1 = 0;
 double Uim1_j = 0;
 double Uip1_j = 0;

 double CoeffYell = 0;
 double CoeffYr = 0;
 double CoeffXell = 0;
 double CoeffXr   = 0; 
 double CoeffAv = 0;

  
  //if()
  if(min_abs(*BloodVesselIndicatorX(i,j))+min_abs(*BloodVesselIndicatorY(i,j))>0.5)
  {
  ////////
  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)	
  {
	  if(i==0)
	  {
	  CoeffXell =0;
	  
	  }
	  else
	  {
	  CoeffXell = *ConductX(i,j);//(*ConductX(i,j)+*ConductX(i-1,j))/2 ;
	 
	  }
	  
	  if(i==Xnodes-1)
	  {
	  CoeffXr   = 0 ;
	  }
	  else
	  {
	  CoeffXr   = *ConductX(i+1,j) ;
	 
	  }
	  if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
	  {
	   if(j==0)
	   {
	   CoeffYell = 0;//(*ConductY(i,j)+*ConductY(i,j))/2 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j);//(*ConductY(i,j)+*ConductY(i,j))/2 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = 0;
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j);
	   CoeffYr   = *ConductY(i,j+1);
	   }
	  }
	  else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
	  {
	  
	  }
	  else if(*BloodVesselIndicatorY(i,j)>0)
	  {
	  	if(j==0)
	   {
	   
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   
	   CoeffYr   = 0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   
	   CoeffYr   = *ConductY(i,j+1); 
	   }
	  }
	  else
	  {
	  	if(j==0)
	   {
	   CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j);
	   
	   }
	   else
	   {
	   CoeffYell =  *ConductY(i,j);	  
	   }
	  }
  }
  else if(min_abs(*BloodVesselIndicatorX(i,j))<0.5)
  {
  	  if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
	  {
	   if(j==0)
	   {
	   CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = *ConductY(i,j+1) ;
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   = 0 ;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   CoeffYell =  *ConductY(i,j) ;
	   CoeffYr   = *ConductY(i,j+1) ;
	   }
	  }
	  else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
	  {
	  
	  }
	  else if(*BloodVesselIndicatorY(i,j)>0)
	  {
	  	if(j==0)
	   {
	   
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   
	   CoeffYr   = 0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   
	   CoeffYr   =*ConductY(i,j+1); 
	   }
	  }
	  else
	  {
	  	if(j==0)
	   {
	   CoeffYell = 0;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j);
	   
	   }
	   else
	   {
	    CoeffYell = *ConductY(i,j);
	  
	   }
	  }
  }
  else if(*BloodVesselIndicatorX(i,j)>0)
  {

	  if(i==Xnodes-1)
	  {
	  CoeffXr   = 0 ;
	  }
	  else
	  {
	  CoeffXr   = *ConductX(i+1,j); 
	  }
      
       if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
	  {
	   if(j==0)
	   {
	   CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   =0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   = *ConductY(i,j+1) ; 
	   }
	  }
	  else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
	  {
	  
	  }
	  else if(*BloodVesselIndicatorY(i,j)>0)
	  {
	  	if(j==0)
	   {
	   
	   CoeffYr   = *ConductY(i,j+1) ;
	   }
	   else if(j==Ynodes-1)
	   {
	   
	   CoeffYr   = 0 ;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   
	   CoeffYr   = *ConductY(i,j+1) ; 
	   }
	  }
	  else
	  {
	  	if(j==0)
	   {
	   CoeffYell = 0 ;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j) ;
	   
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j) ;
	  
	   }
	  }
  }
  else
  {
        if(i==0)
	  {
	  CoeffXell =0;
	  }
	  else
	  {
	  CoeffXell = *ConductX(i,j) ;
	  }

	
       if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
	  {
	   if(j==0)
	   {
	   CoeffYell = 0;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   CoeffYr   = *ConductY(i,j+1) ;
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   = 0;//CoeffYr   = (*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j) ;
	   CoeffYr   = *ConductY(i,j+1) ; 
	   }
	  }
	  else if(min_abs(*BloodVesselIndicatorY(i,j))<0.5)
	  {
	  
	  }
	  else if(*BloodVesselIndicatorY(i,j)>0)
	  {
	  	if(j==0)
	   {
	   
	   CoeffYr   = *ConductY(i,j+1);
	   }
	   else if(j==Ynodes-1)
	   {
	   
	   CoeffYr   = 0 ;//(*ConductY(i,j)+*ConductY(i,0))/2 ;
	   }
	   else
	   {
	   
	   CoeffYr   = *ConductY(i,j+1) ; 
	   }
	  }
	  else
	  {
	  	if(j==0)
	   {
	   CoeffYell = 0;//CoeffYell = (*ConductY(i,j)+*ConductY(i,Ynodes-1))/2 ;
	   
	   }
	   else if(j==Ynodes-1)
	   {
	   CoeffYell = *ConductY(i,j);
	   
	   }
	   else
	   {
	   CoeffYell = *ConductY(i,j);
	  
	   }
	  }
  }
   
	
	 ////// 
	 if(i==0)
     {
		Uim1_j = *Solution(i,j); 
	 }
   else
     {
		Uim1_j = *Solution(i-1,j);  
     }
   if(i==Xnodes-1)
     {
		Uip1_j = *Solution(i,j);
     }
   else
     {
		Uip1_j = *Solution(i+1,j);
     }

	 
	 
   if(j==0)
     {
		Ui_jm1 = *Solution(i,j); //Ui_jm1 = *Solution(i,Ynodes-1);
	 }
   else
     {
		Ui_jm1 = *Solution(i,j-1);  
     }
   if(j==Ynodes-1)
     {
		Ui_jp1 = *Solution(i,j);//Ui_jp1 = *Solution(i,0);
     }
   else
     {
		Ui_jp1 = *Solution(i,j+1);
     }
	
	 *Conduct(i,j) = (CoeffXell+CoeffXr+ CoeffYell+CoeffYr) / *BloodVesselIndicator(i,j);
	 CoeffAv=*Conduct(i,j);
	//if(j!=Ynodes-1)
	//{
	//  cout<<i<<","<<j<<*BloodVesselIndicacd torX(i,j)<<"  "<<*BloodVesselIndicatorY(i,j)<<endl;
	//  cout<<"CoeffXell"<<CoeffXell<<"CoeffXr"<<CoeffXr<<endl;
	  
	 // cout<<"CoeffYell"<<CoeffYell<<"CoeffYr"<<CoeffYr<<endl;
	 if(*Conduct(i,j)>0)
	 {
	
	//}  
	//cout<<"Conduct"<<CoeffAv<<"  "<<CoeffXell<<"  "<<CoeffXr<<"  "<<CoeffYell<<"  "<<CoeffYr<<endl;
	//cout<<" "<<Uim1_j<<" "<<Uip1_j<<" "<<Ui_jp1<<" "<<Ui_jm1<<endl;
	//getchar();
	//cout<<1.85*((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+ p * CoeffAv * *InterPressure(i, j))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr)+ p * CoeffAv  )-0.85*Uij;
	
	OutPut = 1.85*((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+ p * CoeffAv * *InterPressure(i, j))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr)+ p * CoeffAv  )-0.85*Uij;
     }
	 else
	 {
	 OutPut = 0;
	 }
if(!(min_abs(OutPut)<1000000000.0))
 {
 cout<<OutPut<<"i  "<<i<<"j   "<<j<<" Uim1_j "<< Uim1_j<<" Uip1_j "<< Uip1_j<<" Ui_jm1 "<< Ui_jm1<<" Ui_jp1 "<< Ui_jp1<<endl;
 cout<<" left "<< CoeffXell<<" right "<< CoeffXr<<" down "<<CoeffYell<<" up "<<CoeffYr; 
 getchar();
 }	
	
	return  OutPut;
	//return ((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+ p * CoeffAv * *InterPressure(i, j))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr)+ p * CoeffAv  );
  //return 1.85*((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr) )-0.85*Uij;   
 // return ((CoeffXell*Uim1_j + CoeffXr*Uip1_j)+ (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)+(p * r )* *InterPressure(i, j))/((CoeffXell+CoeffXr) + (CoeffYell+CoeffYr)+(p * r) );
  }
  else// single point
  {
  return Uij;
  }
  
   
}

double AngioFluidPressureUpdate( Matrix& Diffusion,  Matrix& BloodNodePressure, Matrix& Solution,   int i, int j)
{

extern Matrix BloodVesselIndicator;
extern Matrix LymIndicator;
extern Matrix Permeability;
extern Matrix Conduct;

 double drain=0.0;
 extern Matrix BloodVesselRadius;

 extern Matrix X;
 extern Matrix Y;
 extern int Xnodes,Ynodes;
 double dx = *X(1)-*X(0);
 double dy = *Y(1)-*Y(0);
 
 double dx2 = square(dx);
 double dy2 = square(dy);
  
 
 double Uij = *Solution(i,j);
 double Uim1_j = *Solution(i-1,j);
 double Uip1_j = *Solution(i+1,j);
 double Ui_jm1 = *Solution(i,j-1);
 double Ui_jp1 = *Solution(i,j+1);
 
 //double dt = 1.0;

 
 double CenterDiffusion = *Diffusion(i,j);

 double CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
 double CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
 double CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
 double CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
 
 double f=0.0;
 double g=*Permeability(i, j)* *Conduct(i, j) ;
 if(*BloodVesselIndicator(i,j)>0.5)
 {
   //f=*Permeability(i, j)* *BloodVesselRadius(i, j) * *BloodNodePressure(i, j);
   f=*Permeability(i, j)* *Conduct(i, j) * *BloodNodePressure(i, j);
	
 }
 
 
if(*LymIndicator(i,j)>0.5)
 {
	g=g+drain;
 }
 

//cout<<"dada"<<endl;
 return((( (CoeffXell*Uim1_j + CoeffXr*Uip1_j)/dx2
              + (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)/dy2+ f )
       /(  (CoeffXell+CoeffXr)/dx2 + (CoeffYell+CoeffYr)/dy2 + g )))*1.85-0.85*Uij;

 
}

double FluidPressureUpdateBoundaryPoint( Matrix& Diffusion,  Matrix& BloodNodePressure, Matrix& Solution,   int i, int j)
{
extern Matrix Conduct;
extern Matrix BloodVesselIndicator;
extern Matrix BloodVesselRadius;
extern Matrix LymIndicator;
extern Matrix Permeability;
extern Matrix X, Y;
extern int Xnodes, Ynodes;
double dx=*X(1)-*X(0);
double dy=*Y(1)-*Y(0);
double dx2 = square(dx);
double dy2 = square(dy);
double dt = 1.0;
double drain=0.0;
double Uij = *Solution(i,j);
double CenterDiffusion = *Diffusion(i,j);
 double Uim1_j = 0;
 double Uip1_j = 0;
 double Ui_jm1 = 0;
 double Ui_jp1 = 0;


 double CoeffXell = 0;
 double CoeffXr   = 0;
 double CoeffYell = 0;
 double CoeffYr   = 0;

 if(i==0)
 {
 if(j == 0)
 {

  Uim1_j = *Solution(i+1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j+1);
  Ui_jp1 = *Solution(i,j+1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 
 }
 else if(j == Solution.TellCols() - 1)
 {
  Uim1_j = *Solution(i+1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j-1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );

 
 }
 else
 {
 
  Uim1_j = *Solution(i+1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j+1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 
 }
 }
 
 if(i==Solution.TellRows() - 1)
 {
 if(j == 0)
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i-1,j);
  Ui_jm1 = *Solution(i,j+1);
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 else if(j == Solution.TellCols() - 1)
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i-1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j-1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );

 }
 else
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i-1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 }
 
 
 if(j==0)
 {
 if(i == 0)
 {
 //done
  Uim1_j = *Solution(i+1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j+1);
  Ui_jp1 = *Solution(i,j+1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 else if(i == Solution.TellRows() - 1)
 {
 //done
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i-1,j);
  Ui_jm1 = *Solution(i,j+1);
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 else
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j+1);
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 }
 
  if(j==Solution.TellCols() - 1)
 {
 if(i == 0)
 {
 //done
  Uim1_j = *Solution(i+1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j-1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
 }
 else if(i == Solution.TellRows() - 1)
 {
 //done
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i-1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j-1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
 }
 else
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j-1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );

 }
 }

 
  double f=0.0;

 if(*BloodVesselIndicator(i,j)>0.5)
 {
	f=*Permeability(i, j)* *Conduct(i, j) * *BloodNodePressure(i, j);
	
 }
 
  double g=*Permeability(i, j)* *Conduct(i, j) ;
 if(*LymIndicator(i,j)>0.5)
 {
	g=g+drain;
 }
 

 return (( (CoeffXell*Uim1_j + CoeffXr*Uip1_j)/dx2 
              + (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)/dy2 + f )
       /(  (CoeffXell+CoeffXr)/dx2 + (CoeffYell+CoeffYr)/dy2 + g ))*1.85-0.85*Uij;



}

double FluidPressureUpdateBoundaryPointD( Matrix& Diffusion,  Matrix& BloodNodePressure, Matrix& Solution,   int i, int j)
{
extern Matrix Conduct;
extern Matrix BloodVesselIndicator;
extern Matrix BloodVesselRadius;
extern Matrix LymIndicator;
extern Matrix Permeability;
extern Matrix X, Y;
extern int Xnodes, Ynodes;
double dx=*X(1)-*X(0);
double dy=*Y(1)-*Y(0);
double dx2 = square(dx);
double dy2 = square(dy);
double dt = 1.0;
double drain=0.0;
double Uij = *Solution(i,j);
double CenterDiffusion = *Diffusion(i,j);
 double Uim1_j = 0;
 double Uip1_j = 0;
 double Ui_jm1 = 0;
 double Ui_jp1 = 0;


 double CoeffXell = 0;
 double CoeffXr   = 0;
 double CoeffYell = 0;
 double CoeffYr   = 0;

 if(i==0)
 {
 if(j == 0)
 {

  Uim1_j = 0;
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = 0;
  Ui_jp1 = *Solution(i,j+1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 
 }
 else if(j == Solution.TellCols() - 1)
 {
  Uim1_j = 0;
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = 0;


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );

 
 }
 else
 {
 
  Uim1_j = 0;
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j+1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 
 }
 }
 
 if(i==Solution.TellRows() - 1)
 {
 if(j == 0)
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = 0;
  Ui_jm1 = 0;
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 else if(j == Solution.TellCols() - 1)
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = 0;
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = 0;
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );

 }
 else
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = 0;
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 }
 
 
 if(j==0)
 {
 if(i == 0)
 {
 //done
  Uim1_j = 0;
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = 0;
  Ui_jp1 = *Solution(i,j+1);


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 else if(i == Solution.TellRows() - 1)
 {
 //done
  Uim1_j = *Solution(i-1,j);
  Uip1_j = 0;
  Ui_jm1 = 0;
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 else
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = 0;
  Ui_jp1 = *Solution(i,j+1);
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j+1) + CenterDiffusion );

 }
 }
 
  if(j==Solution.TellCols() - 1)
 {
 if(i == 0)
 {
 //done
  Uim1_j = 0;
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = 0;


  CoeffXell = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
 }
 else if(i == Solution.TellRows() - 1)
 {
 //done
  Uim1_j = *Solution(i-1,j);
  Uip1_j = 0;
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = 0;
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
 }
 else
 {
  Uim1_j = *Solution(i-1,j);
  Uip1_j = *Solution(i+1,j);
  Ui_jm1 = *Solution(i,j-1);
  Ui_jp1 = 0;
 
  CoeffXell = 0.5*( *Diffusion(i-1,j) + CenterDiffusion );
  CoeffXr   = 0.5*( *Diffusion(i+1,j) + CenterDiffusion );
  CoeffYell = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );
  CoeffYr   = 0.5*( *Diffusion(i,j-1) + CenterDiffusion );

 }
 }

 
  double f=0.0;

 if(*BloodVesselIndicator(i,j)>0.5)
 {
	f=*Permeability(i, j)* *Conduct(i, j) * *BloodNodePressure(i, j);
	
 }
 
  double g=*Permeability(i, j)* *Conduct(i, j) ;
 if(*LymIndicator(i,j)>0.5)
 {
	g=g+drain;
 }
 

 return ((( (CoeffXell*Uim1_j + CoeffXr*Uip1_j)/dx2 
              + (CoeffYell*Ui_jm1 + CoeffYr*Ui_jp1)/dy2 + f )
       /(  (CoeffXell+CoeffXr)/dx2 + (CoeffYell+CoeffYr)/dy2 + g )))*1.85-0.85*Uij;



}

void VesselStructure::GrowSprout_one_step()
{
cout <<"function GrowSprout running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix BloodNodePressure;
extern Matrix VesselCells;
extern Matrix TAF;
extern Matrix ECM;
extern Matrix CellColor;
extern Matrix Shuffle;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
int LoopLength = VesselListLength; 
VesselBroke=0;
int s = 0;
 cout<<"Growth Begin"<<endl;

for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"enter loop"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
	
		if(*Shuffle(i,j)>0)
	{
		//	cout<<"beginkill";
				VesselBroke=1;
				*BloodNodePressure(i,j) = 0.0;
	//tune the indicator matrix
	    if (i == 0) {iminus=i;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=j;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = i;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = j;} else  {jplus = j+1;}
	
	  int I, J;
	  
	  I=iplus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=1;}
	  else if(*BloodVesselIndicatorX(I,J)<-0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	
	  I=iminus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=-1;}
	  else if(*BloodVesselIndicatorX(I,J)>0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
	
	 
	  I=i;
	  J=jplus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=1;}
	  else if(*BloodVesselIndicatorY(I,J)<-0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	  I=i;
	  J=jminus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=-1;}
	  else if(*BloodVesselIndicatorY(I,J)>0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	  I=i;
	  J=j;
	  *BloodVesselIndicatorX(I,J)=0;
	  *BloodVesselIndicatorY(I,J)=0;	 	  
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			  
				
				  
					
	//tune the chain
					  
					  
	VesselPoint* pTempVesselPoint = pCurrentVesselPoint1;
	cout<<"i:"<<i<<",j:"<<j<<"  ";
		touchpoints++;
		cout<<"touchpoints"<<touchpoints<<"  ";

	
	
	if(pCurrentVesselPoint1->pNextVesselPoint)
	{
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	else
	{
	cout<<"the last point"<<endl;
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=NULL;
	//(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint2= pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	 
	   pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	   
	   
	  pTempVesselPoint->pPreviousVesselPoint	= NULL;
	  pTempVesselPoint->pNextVesselPoint = NULL;
	  pTempVesselPoint->i = 0;
	  pTempVesselPoint->j = 0;
	  pTempVesselPoint->AmIArtery = 0;
	  delete pTempVesselPoint;
	  //getchar();

	  
	  
	 /**
	  
	  (pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint1->pPreviousVesselPoint	= NULL;
	 pCurrentVesselPoint1->pNextVesselPoint = NULL;
	cout<<"i:"<<i<<",j:"<<j<<"  ";
		touchpoints++;
		cout<<"touchpoints"<<touchpoints<<"  ";
	delete [] pCurrentVesselPoint1;
	getchar();
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	 
	**/  



	 VesselListLength=VesselListLength-1;
	 
	 
	 if(*SproutTips(i,j)>0)
	 {
	 
	    SproutNumber=SproutNumber-*SproutTips(i,j); 
		*SproutTips(i,j)=0;
		*SproutAge(i,j)=0;
		
	 
	 }
	 else
	 {
	 *VesselAge(i,j)=0;
	 }
	 
	
	
	
	
	
	
	//cout<<"endkill";
	
	
	
	
	
	}//this is for the kill of points
	else
	{
	
	
	
if((i==0||j==0||i==Xnodes-1||j==Ynodes-1)&&*SproutTips(i,j)>0)
	{
	*SproutTips(i,j)=0;
	//SproutNumber -=1;
	}
	
	/******Modi Here*****/
	s = 0;
	if(*VesselCells(i,j)>=1)
	{
	s =  *SproutTips(i,j);
	}
	
	if(s>0)
	{
	s=1;
	}
	
	
	/*****Modi Here*****/
	for(int k=0; k< s; k++ )
	{
	
	
	//cout<<"enter sprouttip loop"<<endl;
		//cout<<"beginsprout";
	    if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
		
	 		//Prob0 should be with stability 
													
	  
		  *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i, j) );
	 
	      *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i, j) );
	
	      *Prob3(i, j) = Chemo_Response*( *TAF(i, jplus) - *TAF(i, j) );	   
    
	      *Prob4(i, j) = Chemo_Response*( *TAF(i, jminus) - *TAF(i, j) );
		  
		   double MakeUpToZero1, MakeUpToZero2, MakeUpToZero3, MakeUpToZero4;
	  
			MakeUpToZero1 = 0;
			MakeUpToZero2 = 0;
			MakeUpToZero3 = 0;
			MakeUpToZero4 = 0;
			
		if( *Prob0(i,j) <= 0 )
		{
		*Prob0(i,j) = 0;
		}
		if( *Prob1(i,j) <= 0 )
		{
		*Prob1(i,j) = 0;
	
		}
        if( *Prob2(i,j) <= 0 )
		{
		*Prob2(i,j) = 0;
	
		}
		if( *Prob3(i,j) <= 0 )
		{
		*Prob3(i,j) = 0;
	
		}

		if( *Prob4(i,j) <= 0 )
		{
		*Prob4(i,j) = 0;
	
		}

       
	   
	   
	   
	 //the sprout shouldn't grow reverse  
	   if (min_abs(*BloodVesselIndicatorX(i,j)) > 1.5 )
	   {
	   *Prob2(i,j) = 0;
	   *Prob1(i,j) = 0;
	   
	   }
	   else if (*BloodVesselIndicatorX(i,j) > 0)
	   {
	   *Prob1(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorX(i,j) < 0)
	   {
	   *Prob2(i,j) = 0;
	   }
	   
	   if (min_abs(*BloodVesselIndicatorY(i,j)) > 1.5 )
	   {
	   *Prob3(i,j) = 0;
	   *Prob4(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) < 0 )
	   {
	   *Prob4(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) > 0 )
	   {
	   *Prob3(i,j) = 0;
	   }
	   //other exceptions 
		if (i == Xnodes-1) 
	    { *Prob1(i, j) = 0.0; }
		else if (i == 0)   
	    { *Prob2(i, j) = 0.0; }
		if (j == ( Ynodes-1))
	    { *Prob3(i, j) = 0.0; }
		 if (j == 0)
	    { *Prob4(i, j) = 0.0; }


   // cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
     //rescale the probabilities
	 double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
	 if ( ProbTotal> 0 )
	 {
		*Prob0(i,j) /= ProbTotal;
		*Prob1(i,j) /= ProbTotal;
		*Prob2(i,j) /= ProbTotal;
		*Prob3(i,j) /= ProbTotal;
		*Prob4(i,j) /= ProbTotal;
	 }
	// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 
	 //Get Random Intervals
	 *Inf0(i,j) = 0;
	 *Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
	 //*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
	 *Inf1(i,j) = *Sup0(i,j);
	 *Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
	 //*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
	 *Inf2(i,j) = *Sup1(i,j);
	 *Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
	 *Inf3(i,j) = *Sup2(i,j);
	 *Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
	 //*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
	 *Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
	 *Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
	 //*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
	 
	 //Done with those probabilities, begin to Move and Connect in one step.
 double RandNumber;
	 int I, J;
	 RandNumber = float( rand() ) / float( RAND_MAX);

	 if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
	 {
		
	 }
	 
	 else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
	 {
		I = i + 1;
		J = j;
	
	
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		
	     if( *SproutTips(I, J) > 0) 
		  {
			
		 *VesselCells(i,j) +=1;				
		 *VesselCells(I,J) +=1;								
		 *SproutTips(i, j) -=1;
		 *SproutTips(I, J) -=1;												
		  }
		  else
		  {
		 *SproutTips(i, j) -=1;
		 *VesselCells(I,J) +=1;
		  CellNumber+=1;
		  SproutNumber-=1;
		  }
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		
		 	
				  
		   
			
			 
			   
		}
		else
		{
		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) -=1;
		*SproutTips(I, J) += 1;
	
		//*DirectionX(I, J) = 1;
		
		
		
		
		
		
		}
		
	
		
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
		
		
	 }
     
	 else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
	 {
		I = i - 1;
		J = j;
	
		
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		
	     if( *SproutTips(I, J) > 0) 
		  {
			
		 *VesselCells(i,j) +=1;				
		 *VesselCells(I,J) +=1;								
		 *SproutTips(i, j) -=1;
		 *SproutTips(I, J) -=1;												
		  }
		  else
		  {
		 *SproutTips(i, j) -=1;
		 *VesselCells(I,J) +=1;
		  CellNumber+=1;
		  SproutNumber-=1;
		  }
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		
		 	
				  
		   
			
			 
			   
		}
		else
		{
		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) -=1;
		*SproutTips(I, J) += 1;
	
		//*DirectionX(I, J) = 1;
		
		
		
		
		
		
		}
		
		
	
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }
	 
	 else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
	 {
		I = i;
		J = j + 1;
	
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		
	      if( *SproutTips(I, J) > 0) 
		  {
			
		 *VesselCells(i,j) +=1;				
		 *VesselCells(I,J) +=1;								
		 *SproutTips(i, j) -=1;
		 *SproutTips(I, J) -=1;												
		  }
		  else
		  {
		 *SproutTips(i, j) -=1;
		 *VesselCells(I,J) +=1;
		  CellNumber+=1;
		  SproutNumber-=1;
		  }	  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		
		 	
				  
		   
			
			 
			   
		}
		else
		{
		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) -=1;
		*SproutTips(I, J) += 1;
	
		//*DirectionX(I, J) = 1;
		
		
		
		
		
		
		}
		

	
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }
	 
	 else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
	 {
		I = i;
		J = j - 1;
	
				
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		
	     if( *SproutTips(I, J) > 0) 
		  {
			
		 *VesselCells(i,j) +=1;				
		 *VesselCells(I,J) +=1;								
		 *SproutTips(i, j) -=1;
		 *SproutTips(I, J) -=1;												
		  }
		  else
		  {
		 *SproutTips(i, j) -=1;
		 *VesselCells(I,J) +=1;
		  CellNumber+=1;
		  SproutNumber-=1;
		  }		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		
				  
		   
			
			 
			   
		}
		else
		{
		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) -=1;
		*SproutTips(I, J) += 1;
	
		//*DirectionX(I, J) = 1;
		
		
		
		
		
		
		}
		
	
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }

	//BOUNDARY SPROUTS We can ask it to be periodical?
    }//this is for the if sprout
	
	}//this is for not shuffle		
	
	pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	 //cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
    QuickPlot();// COMBINE
   cout <<"function GrowSprout finished"<<endl; 
 }
	
 
 double min_prob(double a, int b, int N)
 {
 double senseTAF = 0.5;
 double senseVolume = 1;

 double p = exp(senseTAF*a/MaxTAF)/exp(senseVolume*(N-b));
 /*
 if(p>1)
 {
 p = 1;
 }
 */
 if(b<N)
 {
 p = 0;
 }
 else
 {
 p = 1;
 }
 
  return p;
 }

void VesselStructure::GrowSprout(Matrix& Shuffle)
{
cout <<"function GrowSprout running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix BloodNodePressure;
extern Matrix TAF;
extern Matrix ECM;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
//sweep the list to update rather than matrix?
//yes!
//should we have more list?
//we don't need to, because even if we have two main vessel,
//it's like a whole system with the general BC and IC.

//DON'T FORGET THE INDICATOR
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list

int LoopLength = VesselListLength; 
VesselBroke=0;
cout<<"Growth Begin"<<endl;
for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"enter loop"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
	
 // cout<<i<<"    "<<j<<endl;
	//if(*Shuffle(i,j)>0&&pCurrentVesselPoint1->AmIArtery==0)
	if((*Shuffle(i,j)>0&&*VesselAge(i,j)>=VesselAgeInf))
	{
		//	cout<<"beginkill";
				VesselBroke=1;
	//tune the indicator matrix
	    if (i == 0) {iminus=i;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=j;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = i;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = j;} else  {jplus = j+1;}
	
	  int I, J;
	  
	  I=iplus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=1;}
	  else if(*BloodVesselIndicatorX(I,J)<-0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	
	  I=iminus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=-1;}
	  else if(*BloodVesselIndicatorX(I,J)>0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
	
	 
	  I=i;
	  J=jplus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=1;}
	  else if(*BloodVesselIndicatorY(I,J)<-0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	  I=i;
	  J=jminus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=-1;}
	  else if(*BloodVesselIndicatorY(I,J)>0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	  I=i;
	  J=j;
	  *BloodVesselIndicatorX(I,J)=0;
	  *BloodVesselIndicatorY(I,J)=0;	 	  
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			  
				
				  
					
	//tune the chain
					  
					  
	VesselPoint* pTempVesselPoint = pCurrentVesselPoint1;
	cout<<"i:"<<i<<",j:"<<j<<"  ";
		touchpoints++;
		cout<<"touchpoints"<<touchpoints<<"  ";

	
	
	if(pCurrentVesselPoint1->pNextVesselPoint)
	{
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	else
	{
	cout<<"the last point"<<endl;
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=NULL;
	//(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint2= pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	 
	   pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	   
	   
	  pTempVesselPoint->pPreviousVesselPoint	= NULL;
	  pTempVesselPoint->pNextVesselPoint = NULL;
	  pTempVesselPoint->i = 0;
	  pTempVesselPoint->j = 0;
	  pTempVesselPoint->AmIArtery = 0;
	  delete pTempVesselPoint;
	  //getchar();

	  
	  
	 /**
	  
	  (pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint1->pPreviousVesselPoint	= NULL;
	 pCurrentVesselPoint1->pNextVesselPoint = NULL;
	cout<<"i:"<<i<<",j:"<<j<<"  ";
		touchpoints++;
		cout<<"touchpoints"<<touchpoints<<"  ";
	delete [] pCurrentVesselPoint1;
	getchar();
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	 
	**/  



	 VesselListLength=VesselListLength-1;
	 
	 
	 if(*SproutTips(i,j)>0)
	 {
		*SproutTips(i,j)=0;
		*SproutAge(i,j)=0;
		SproutNumber=SproutNumber-1;
	 
	 }
	 else
	 {
	 *VesselAge(i,j)=0;
	 }
	 
	
	
	
	
	
	
	//cout<<"endkill";
	
	
	
	
	
	}//this is for the kill of points
	else
	{
    
	if(i==0&&*SproutTips(i,j)==1)
	{
	*SproutTips(i,j)=0;
	SproutNumber -=1;
	}
	else if(i==Xnodes-1&&*SproutTips(i,j)==1)
	{
	*SproutTips(i,j)=0;
	SproutNumber -=1;
	}
	
	if( *SproutTips(i,j) > 0)
	{
	//cout<<"enter sprouttip loop"<<endl;
		//cout<<"beginsprout";
	    if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
		
	       /****  			 
		*Prob0(i, j) = 1.0
	                 - 2.0 * Dif_ECM * AngioDeltaT * ( (1.0/pow(AngioDeltaX,2) ) + (1.0/pow(AngioDeltaY,2) ) )
					 + ( ( Chemo_Response * Chemo_Sense * AngioDeltaT ) / pow( ( 1.0 + Chemo_Sense * *TAF(i,j) ), 2) ) 
						* ( (pow( ( *TAF(iplus, j) - *TAF(iminus, j) ), 2) / pow( (2.0*AngioDeltaX), 2) )
	                       +(pow( ( *TAF(i, jplus) - *TAF(i, jminus) ), 2) / pow( (2.0*AngioDeltaY), 2) ) ) 
                     - ( (Chemo_Response * AngioDeltaT) / ( 1.0 + Chemo_Sense* *TAF(i, j) ) )
	                    *( ( ( *TAF(iplus, j) - 2.0 * *TAF(i, j) + *TAF(iminus, j) ) / pow(AngioDeltaX, 2) )
                          +( ( *TAF(i, jplus) - 2.0 * *TAF(i, j) + *TAF(i, jminus) ) / pow(AngioDeltaY, 2) ) )
	                 - Haptotactic_Reponse * AngioDeltaT * ( ( ( *ECM(iplus, j) - 2.0 * *ECM(i, j) + *ECM(iminus, j) ) / pow(AngioDeltaX, 2) )
	                                         +( ( *ECM(i, jplus) - 2.0 * *ECM(i, j) + *ECM(i, jminus) ) / pow(AngioDeltaY, 2) ) );
											 
	  
		  *Prob1(i, j) = ( Dif_ECM * AngioDeltaT ) / pow( AngioDeltaX, 2 )
						+ ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(iplus, j) - *TAF(i, j) )
		                      + Haptotactic_Reponse * ( *ECM(iplus, j) - *ECM(i, j) ) );
		
		


	 
	
	     *Prob2(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaX, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, j) - *TAF(iminus, j) )
		                      + Haptotactic_Reponse * ( *ECM(i, j) - *ECM(iminus, j) ) );
	
	
	
	     *Prob3(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       + ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, jplus) - *TAF(i, j) )
		                      + Haptotactic_Reponse * ( *ECM(i, jplus) - *ECM(i, j) ) );
	
	   
    
	     *Prob4(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, j) - *TAF(i, jminus) )
		                      + Haptotactic_Reponse * ( *ECM(i, j) - *ECM(i, jminus) ) );
		  *********/
		 
		*Prob0(i, j) = 1.0
	                 - 2.0 * Dif_ECM * AngioDeltaT * ( (1.0/pow(AngioDeltaX,2) ) + (1.0/pow(AngioDeltaY,2) ) )
					 + ( ( Chemo_Response * Chemo_Sense * AngioDeltaT ) / pow( ( 1.0 + Chemo_Sense * *TAF(i,j) ), 2) ) 
						* ( (pow( ( *TAF(iplus, j) - *TAF(iminus, j) ), 2) / pow( (2.0*AngioDeltaX), 2) )
	                       +(pow( ( *TAF(i, jplus) - *TAF(i, jminus) ), 2) / pow( (2.0*AngioDeltaY), 2) ) ) 
                     - ( (Chemo_Response * AngioDeltaT) / ( 1.0 + Chemo_Sense* *TAF(i, j) ) )
	                    *( ( ( *TAF(iplus, j) - 2.0 * *TAF(i, j) + *TAF(iminus, j) ) / pow(AngioDeltaX, 2) )
                          +( ( *TAF(i, jplus) - 2.0 * *TAF(i, j) + *TAF(i, jminus) ) / pow(AngioDeltaY, 2) ) )
	                 - Haptotactic_Reponse * AngioDeltaT * ( ( ( *ECM(iplus, j) - 2.0 * *ECM(i, j) + *ECM(iminus, j) ) / pow(AngioDeltaX, 2) )
	                                         +( ( *ECM(i, jplus) - 2.0 * *ECM(i, j) + *ECM(i, jminus) ) / pow(AngioDeltaY, 2) ) );
											 
	  
		  *Prob1(i, j) = ( Dif_ECM * AngioDeltaT ) / pow( AngioDeltaX, 2 )
						+ ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(iplus, j) - *TAF(iminus, j) )
		                      + Haptotactic_Reponse * ( *ECM(iplus, j) - *ECM(iminus, j) ) );
		
		


	 
	
	     *Prob2(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaX, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaX, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(iplus, j) - *TAF(iminus, j) )
		                      + Haptotactic_Reponse * ( *ECM(iplus, j) - *ECM(iminus, j) ) );
	
	
	
	     *Prob3(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       + ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, jplus) - *TAF(i, jminus) )
		                      + Haptotactic_Reponse * ( *ECM(i, jplus) - *ECM(i, jminus) ) );
	
	   
    
	     *Prob4(i, j) = ( Dif_ECM*AngioDeltaT ) / pow( AngioDeltaY, 2 )
                       - ( AngioDeltaT / ( 4.0 * pow( AngioDeltaY, 2 ) ) )
		                   *( ( Chemo_Response/( 1.0+Chemo_Sense * *TAF(i, j) ) ) * ( *TAF(i, jplus) - *TAF(i, jminus) )
		                      + Haptotactic_Reponse * ( *ECM(i, jplus) - *ECM(i, jminus) ) );
							  
							

   //cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 // make probabilities above 0  
	   double MakeUpToZero1, MakeUpToZero2, MakeUpToZero3, MakeUpToZero4;
	  
			MakeUpToZero1 = 0;
			MakeUpToZero2 = 0;
			MakeUpToZero3 = 0;
			MakeUpToZero4 = 0;
			
		if( *Prob0(i,j) < 0 )
		{
		*Prob0(i,j) = 0;
		}
		if( *Prob1(i,j) < 0 )
		{
		MakeUpToZero1 = min_abs(*Prob1(i,j));
	
		}
		if( *Prob2(i,j) < 0 )
		{
		MakeUpToZero2 = min_abs(*Prob2(i,j));
		
		}
		if( *Prob3(i,j) < 0 )
		{
		MakeUpToZero3 = min_abs(*Prob3(i,j));
		
		}
		if( *Prob4(i,j) < 0 )
		{
		MakeUpToZero4 = min_abs(*Prob4(i,j));
		
		}
       
	   *Prob1(i,j) += (MakeUpToZero1 + MakeUpToZero2);
	   *Prob2(i,j) += (MakeUpToZero1 + MakeUpToZero2);
	   *Prob3(i,j) += (MakeUpToZero3 + MakeUpToZero4);
	   *Prob4(i,j) += (MakeUpToZero3 + MakeUpToZero4);
	   
	   
	 //the sprout shouldn't grow reverse  
	   if (*BloodVesselIndicatorX(i,j) < 0 )
	   {
	   *Prob2(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorX(i,j) > 0)
	   {
	   *Prob1(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) < 0 )
	   {
	   *Prob4(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) > 0 )
	   {
	   *Prob3(i,j) = 0;
	   }
	   //other exceptions 
		if (i == Xnodes-1) 
	    { *Prob1(i, j) = 0.0; }
		else if (i == 0)   
	    { *Prob2(i, j) = 0.0; }
		if (j == ( Ynodes-1))
	    { *Prob3(i, j) = 0.0; }
		 if (j == 0)
	    { *Prob4(i, j) = 0.0; }


   // cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
     //rescale the probabilities
	 double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
	 if ( ProbTotal> 0 )
	 {
		*Prob0(i,j) /= ProbTotal;
		*Prob1(i,j) /= ProbTotal;
		*Prob2(i,j) /= ProbTotal;
		*Prob3(i,j) /= ProbTotal;
		*Prob4(i,j) /= ProbTotal;
	 }
	// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 
	 //Get Random Intervals
	 *Inf0(i,j) = 0;
	 *Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
	 //*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
	 *Inf1(i,j) = *Sup0(i,j);
	 *Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
	 //*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
	 *Inf2(i,j) = *Sup1(i,j);
	 *Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
	 *Inf3(i,j) = *Sup2(i,j);
	 *Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
	 //*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
	 *Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
	 *Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
	 //*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
	 
	 //Done with those probabilities, begin to Move and Connect in one step.
	 
	 double RandNumber;
	 int I, J;
	 RandNumber = float( rand() ) / float( RAND_MAX);
	 if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
	 {
		I = i;
		J = j;
		*SproutAge(I, J) += AngioDeltaT;
	 }
	 
	 else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
	 {
		I = i + 1;
		J = j;
		//if(*Shuffle(I,J)<0.5)
		{
		
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
	     if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 *SproutTips(i, j) = 0;
		 //*DirectionX(I, J) = 1;
		 *SproutTips(I, J) = 0;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	 
		}
		else
		{
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = 1;
		}
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
		}
		
	 }
     
	 else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
	 {
		I = i - 1;
		J = j;
		//if(*Shuffle(I,J)<0.5)
		{
		
		if(  (min_abs(*BloodVesselIndicator(I, J))) > 0 )
		{
		
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			SproutNumber -= 1;	
			
	
		  }
		 
		  		  
		 *SproutTips(i, j) = 0;
		// *DirectionX(I, J) = -1;
		 *SproutTips(I, J) = 0;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		 //SproutNumber -= 1;
	 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob2 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = -1;
		}
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		}
	 }
	 
	 else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
	 {
		I = i;
		J = j + 1;
		//if(*Shuffle(I,J)<0.5)
		{
		
		if( (*BloodVesselIndicator(I, J))> 0)
		{
		 
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
				SproutNumber -= 1;
				  

		  }
		 
			  
		 *SproutTips(i, j) = 0;
		// SproutNumber -= 1;
		// *DirectionY(I, J) = 1;
		 *SproutTips(I, J) = 0;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob3 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = 1;
		}
		
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		}
	 }
	 
	 else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
	 {
		I = i;
		J = j - 1;
		//if(*Shuffle(I,J)<0.5)
		{
				
		if( (*BloodVesselIndicator(I, J)) > 0)
		{
		 
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			 SproutNumber -= 1;
		  }
		 		  
		 *SproutTips(i, j) = 0;
		// *DirectionY(I, J) = -1;
		 *SproutTips(I, J) = 0;
		 //SproutNumber -= 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		}
		else
		{		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob4 make Vessel Length"<<VesselListLength<<endl;

		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = -1;
		}
		
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		}
	 }

	//BOUNDARY SPROUTS We can ask it to be periodical?
	
	 if(I == 0 || I == Xnodes - 1 || J == 0 || J == Ynodes - 1)
	 {
	  *SproutTips(I, J) = 0;
	  *SproutAge(I, J) = 0;
	 }
	 
	  // cout<<"quit sprouttip loop"<<endl;
	//cout<<"finishsprout";
	 
	 }//this is for the if
	 else
	 {
	//	cout<<"Donothing";
		*VesselAge(i,j) += AngioDeltaT; 
		//cout<<"Age"<<*VesselAge(i,j);
	 }
	 
	 }//this is for the growth of sprout
	 //cout<<"beginmove";
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	 //cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   QuickPlot();// COMBINE
   cout <<"function GrowSprout finished"<<endl; 
 }


void VesselStructure::GrowSprout_onecell(CellVesselStructure* TheVessel)
{
cout <<"function GrowSprout running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix Stability;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
double Stable_down = RetrieveDoubleParameter( ParameterFile , "Stable_down");
extern Matrix TAF;
extern Matrix ECM;
extern Matrix CellColor;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double String = RetrieveDoubleParameter( ParameterFile , "String");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
int growstill=0;
bool setorigin=0;
int LoopLength = VesselListLength; 
 cout<<"Growth Begin"<<endl;
 VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
VesselCell* pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
VesselCell* pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
VesselCell* pTempVesselCell = TheVessel->pFirstVesselCell;

while(growstill<3)
{

cout<<"Once Begin"<<endl;
growstill++;


 Prob0 = zeros(Xnodes, Ynodes);
 Prob1 = zeros(Xnodes, Ynodes);
 Prob2 = zeros(Xnodes, Ynodes);
 Prob3 = zeros(Xnodes, Ynodes);
 Prob4 = zeros(Xnodes, Ynodes);
 Inf0 = zeros(Xnodes, Ynodes);
 Inf1 = zeros(Xnodes, Ynodes);
 Inf2 = zeros(Xnodes, Ynodes);
 Inf3 = zeros(Xnodes, Ynodes);
 Inf4 = zeros(Xnodes, Ynodes);
 Sup0 = zeros(Xnodes, Ynodes);
 Sup1 = zeros(Xnodes, Ynodes);
 Sup2 = zeros(Xnodes, Ynodes);
 Sup3 = zeros(Xnodes, Ynodes);
 Sup4 = zeros(Xnodes, Ynodes);


pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
pTempVesselCell = TheVessel->pFirstVesselCell;
LoopLength = VesselListLength;
VesselBroke=0;


for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"enter loop"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
	int i_origin = pCurrentVesselPoint1->i_origin;
	int j_origin = pCurrentVesselPoint1->j_origin;

	
	
if((i==0||j==0||i==Xnodes-1||j==Ynodes-1)&&*SproutTips(i,j)==1)
	{
	*SproutTips(i,j)=0;
	//SproutNumber -=1;
	}
	
	
	if( *SproutTips(i,j) > 0)
	{
	
	
	//cout<<"enter sprouttip loop"<<endl;
		//cout<<"beginsprout";
	    if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
		
	 		//Prob0 should be with stability 
													
	  /*
		  *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i, j) );
	 
	      *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i, j) );
	
	      *Prob3(i, j) = Chemo_Response*( *TAF(i, jplus) - *TAF(i, j) );	   
    
	      *Prob4(i, j) = Chemo_Response*( *TAF(i, jminus) - *TAF(i, j) );
		  
	 */
	 
		*Prob0(i, j) = 0;
		*Prob1(i, j) = 0;
		*Prob2(i, j) = 0;
		*Prob3(i, j) = 0;
		*Prob4(i, j) = 0;
		if( *TAF(iplus, j) - *TAF(i, j) > 0.0)
		{
		  *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i_origin, j_origin) ) - String*pow((iplus-i_origin)*(iplus-i_origin)+(j-j_origin)*(j-j_origin), 0.5);
	  // cout<< "*Prob1(i, j)"<<*Prob1(i, j)<<endl;
		}
		if(*TAF(iminus, j) - *TAF(i, j) > 0.0)
		{
	      *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i_origin, j_origin) ) - String*pow((iminus-i_origin)*(iminus-i_origin)+(j-j_origin)*(j-j_origin), 0.5);
	//	  cout<< "*Prob2(i, j)"<<*Prob2(i, j)<<endl;
		}
		if(*TAF(i, jplus) - *TAF(i, j) > 0.0)
		{
	//	cout<< "*Prob3(i, j)"<<*Prob3(i, j)<<endl;
	      *Prob3(i, j) = Chemo_Response*( *TAF(i,jplus) - *TAF(i_origin, j_origin) )  - String*pow((i-i_origin)*(i-i_origin)+(jplus-j_origin)*(jplus-j_origin), 0.5);	   
		}
		if(*TAF(i, jminus) - *TAF(i, j) > 0.0)
		{
		
	//	cout<< "*Prob4(i, j)"<<*Prob4(i, j)<<endl;
	      *Prob4(i, j) = Chemo_Response*(  *TAF(i,jminus) - *TAF(i_origin, j_origin) ) - String*pow((i-i_origin)*(i-i_origin)+(jminus-j_origin)*(jminus-j_origin), 0.5);
	    }
		  
		  
		  
		
			
		if( *Prob0(i,j) <= 0 )
		{
		*Prob0(i,j) = 0;
		}
		if( *Prob1(i,j) <= 0 )
		{
		*Prob1(i,j) = 0;
	
		}
        if( *Prob2(i,j) <= 0 )
		{
		*Prob2(i,j) = 0;
	
		}
		if( *Prob3(i,j) <= 0 )
		{
		*Prob3(i,j) = 0;
	
		}

		if( *Prob4(i,j) <= 0 )
		{
		*Prob4(i,j) = 0;
	
		}

       
	   
	   
	   
	 //the sprout shouldn't grow reverse  
	   if (*BloodVesselIndicatorX(i,j) < 0 )
	   {
	   *Prob2(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorX(i,j) > 0)
	   {
	   *Prob1(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) < 0 )
	   {
	   *Prob4(i,j) = 0;
	   }
	   else if (*BloodVesselIndicatorY(i,j) > 0 )
	   {
	   *Prob3(i,j) = 0;
	   }
	   //other exceptions 
		if (i == Xnodes-1) 
	    { *Prob1(i, j) = 0.0; }
		else if (i == 0)   
	    { *Prob2(i, j) = 0.0; }
		if (j == ( Ynodes-1))
	    { *Prob3(i, j) = 0.0; }
		 if (j == 0)
	    { *Prob4(i, j) = 0.0; }


   // cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
     //rescale the probabilities
	 double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
	 if ( ProbTotal> 0 )
	 {
		//growstill = 1;
		
		*Prob0(i,j) /= ProbTotal;
		*Prob1(i,j) /= ProbTotal;
		*Prob2(i,j) /= ProbTotal;
		*Prob3(i,j) /= ProbTotal;
		*Prob4(i,j) /= ProbTotal;
	 
	// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
	 
	 //Get Random Intervals
	 *Inf0(i,j) = 0;
	 *Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
	 //*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
	 *Inf1(i,j) = *Sup0(i,j);
	 *Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
	 //*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
	 *Inf2(i,j) = *Sup1(i,j);
	 *Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
	 *Inf3(i,j) = *Sup2(i,j);
	 *Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
	 //*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
	 *Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
	 *Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
	 //*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
	 
	 //Done with those probabilities, begin to Move and Connect in one step.
 double RandNumber;
	 int I, J;
	 RandNumber = float( rand() ) / float( RAND_MAX);

	 if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
	 {
		
	 }
	 
	 else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
	 {
		I = i + 1;
		J = j;
	
	
		if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
		{
		
		
		if(*Stability(I,J)>0)
		{
           *Stability(I,J) = *Stability(I,J) - AngioDeltaT * Stable_down;
		   if(*Stability(I,J) < 0)
          {
			*Stability(I,J) = 0;
		  }
		  
		  
		
		}
		else
		{
	     if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
			SproutNumber -= 1;//comment this we have anasmosis
			
		  }
		  
		  		  
		 
		 //*DirectionX(I, J) = 1;
		 //*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
		 //SproutNumber -=1;//Add this we have anasmosis
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;
		 *SproutTips(i, j) = 0;	
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		 
		} 
		   
			
			 
			   
		}
		else
		{
		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->i_origin = i_origin;
		pCurrentVesselPoint2->j_origin = j_origin;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = 1;
		
		pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		
		double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
		double sezi;
		sezi= float( rand() ) / float( RAND_MAX);
		if(sezi<Prob)
		{
		
		TheVessel->CellListLength++;
		VesselCell* pNewVesselCell = new VesselCell;
	    pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
	    pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
		pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
		pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
		pCurrentVesselCell2->cCover = 1;
		pCurrentVesselCell2->i[0] = I;
		pCurrentVesselCell2->j[0] = J;
		pCurrentVesselCell2->pNextVesselCell = NULL;
		//pCurrentVesselCell2->cConnection = 1;
	    //pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
		Connect(pCurrentVesselCell2,pCurrentVesselCell1);
		*CellColor(I,J) = 100;
			
		
		}//mitosis and grow
		else
		{
		
		pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
		pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
		pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
		
		*CellColor(I,J) = 100;
		//if(pCurrentVesselCell1->cCover==pCurrentVesselCell1->coverlimit)
		//{
		//*SproutTips(I,J)=0;
		//}
		
		}//justgrow
		
		
		
		
		
		}
		
	
		
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
		
		
	 }
     
	 else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
	 {
		I = i - 1;
		J = j;
	
		
		if(  (min_abs(*BloodVesselIndicator(I, J))) > 0 )
		{
		
		
		if(*Stability(I,J)>0)
		{
           *Stability(I,J) = *Stability(I,J) - AngioDeltaT * Stable_down;
		   if(*Stability(I,J) < 0)
          {
			*Stability(I,J) = 0;
		  }
		  
		  
		
		}
		else
		{
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			SproutNumber -= 1;	
			
	
		  }
		 
		  		  
		// *DirectionX(I, J) = -1;
		// *SproutTips(I, J) = 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		 //SproutNumber -= 1;
		 *SproutTips(i, j) = 0;
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 Connect(pCurrentVesselCell1,pTempVesselCell);
	      }
	 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->i_origin = i_origin;
		pCurrentVesselPoint2->j_origin = j_origin;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob2 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		//*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionX(I, J) = -1;
		
		
		pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		
		double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
		double sezi;
		sezi= float( rand() ) / float( RAND_MAX);
		if(sezi<Prob)
		{
		
		TheVessel->CellListLength++;
		VesselCell* pNewVesselCell = new VesselCell;
	    pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
	    pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
		pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
		pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
		pCurrentVesselCell2->cCover = 1;
		pCurrentVesselCell2->i[0] = I;
		pCurrentVesselCell2->j[0] = J;
		pCurrentVesselCell2->pNextVesselCell = NULL;
		//pCurrentVesselCell2->cConnection = 1;
	    //pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
		Connect(pCurrentVesselCell2,pCurrentVesselCell1);
		*CellColor(I,J) = 100;
		
		}//mitosis and grow
		else
		{
		
		pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
		pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
		pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
		
		*CellColor(I,J) = 100;
		//if(pCurrentVesselCell1->cCover==pCurrentVesselCell1->coverlimit)
		//{
		//*SproutTips(I,J)=0;
		//}
		
		}//justgrow
		
		
		
		}
		
	
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }
	 
	 else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
	 {
		I = i;
		J = j + 1;
	
		if( (*BloodVesselIndicator(I, J))> 0)
		{
		 
		 if(*Stability(I,J)>0)
		{
           *Stability(I,J) = *Stability(I,J) - AngioDeltaT * Stable_down;
		   if(*Stability(I,J) < 0)
          {
			*Stability(I,J) = 0;
		  }
		  
		  
		
		}
		else
		{
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
				SproutNumber -= 1;
				  

		  }
		 
			  
		
		// SproutNumber -= 1;
		// *DirectionY(I, J) = 1;
		 //*SproutTips(I, J) = 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	 
		 *SproutTips(i, j) = 0;
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 Connect(pCurrentVesselCell1,pTempVesselCell);
		}
		 
		}
		else
		{
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->i_origin = i_origin;
		pCurrentVesselPoint2->j_origin = j_origin;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob3 make Vessel Length"<<VesselListLength<<endl;
		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		//*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = 1;
		
		
		//cout<<"here"<<endl;
		pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		//cout<<pCurrentVesselCell1->cIndex<<endl;
		
		double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
		double sezi;
		sezi= float( rand() ) / float( RAND_MAX);
		if(sezi<Prob)
		{
		
		TheVessel->CellListLength++;
		VesselCell* pNewVesselCell = new VesselCell;
	    pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
	    pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
		pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
		pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
		pCurrentVesselCell2->cCover = 1;
		pCurrentVesselCell2->i[0] = I;
		pCurrentVesselCell2->j[0] = J;
		pCurrentVesselCell2->pNextVesselCell = NULL;
		//cout<<pCurrentVesselCell2->cIndex<<endl;
		//pCurrentVesselCell2->cConnection = 1;
	    //pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
		
		Connect(pCurrentVesselCell2,pCurrentVesselCell1);
		*CellColor(I,J) = 100;

		
		
		}//mitosis and grow
		else
		{
		
		pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
		pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
		pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
		
		*CellColor(I,J) = 100;
		//if(pCurrentVesselCell1->cCover==pCurrentVesselCell1->coverlimit)
		//{
		//*SproutTips(I,J)=0;
		//}
		
		}//justgrow
		
		}
	
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }
	 
	 else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
	 {
		I = i;
		J = j - 1;
	
				
		if( (*BloodVesselIndicator(I, J)) > 0)
		{
		
		if(*Stability(I,J)>0)
		{
           *Stability(I,J) = *Stability(I,J) - AngioDeltaT * Stable_down;
		   if(*Stability(I,J) < 0)
          {
			*Stability(I,J) = 0;
		  }
		  
		  
		
		}
		else
		{
		 
		  if( *SproutTips(I, J) > 0) 
		  {
			*VesselAge(I, J) -= AngioDeltaT; 
			 SproutNumber -= 1;
		  }
		 		  
		 
		// *DirectionY(I, J) = -1;
		// *SproutTips(I, J) = 1;
		 //SproutNumber -= 1;
		 *SproutAge(i, j) = 0;
		 *SproutAge(I, J) = 0;	
		 *SproutTips(i, j) = 0;
		 pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		 pTempVesselCell = TheVessel->FindCell(I,J);
		 Connect(pCurrentVesselCell1,pTempVesselCell);

		}
		 
		}
		else
		{		
		
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
		
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->i_origin = i_origin;
		pCurrentVesselPoint2->j_origin = j_origin;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		//cout<<"Prob4 make Vessel Length"<<VesselListLength<<endl;

		
		*SproutTips(i, j) = 0;
		*SproutTips(I, J) = 1;
		//*SproutTips(I, J) = 1;
		*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
		*SproutAge(i, j) = 0;
		//*DirectionY(I, J) = -1;
		
		pCurrentVesselCell1 = TheVessel->FindCell(i,j);
		
		double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
		double sezi;
		sezi= float( rand() ) / float( RAND_MAX);
		if(sezi<Prob)
		{
		
		TheVessel->CellListLength++;
		VesselCell* pNewVesselCell = new VesselCell;
	    pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
	    pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
		pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
		pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
		pCurrentVesselCell2->cCover = 1;
		pCurrentVesselCell2->i[0] = I;
		pCurrentVesselCell2->j[0] = J;
		pCurrentVesselCell2->pNextVesselCell = NULL;
		//pCurrentVesselCell2->cConnection = 1;
	    //pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
		Connect(pCurrentVesselCell2,pCurrentVesselCell1);
		*CellColor(I,J) = 100;
		
		
		}//mitosis and grow
		else
		{
		
		pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
		pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
		pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
		
		*CellColor(I,J) = 100;
		//if(pCurrentVesselCell1->cCover==pCurrentVesselCell1->coverlimit)
		//{
		//*SproutTips(I,J)=0;
		//}
		
		}//justgrow
		
		
		}
	
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		
	 }

	//BOUNDARY SPROUTS We can ask it to be periodical?
    }//probtotal>0
	else
	{
	//*SproutTips(i,j) = 0;
	}//probtotal=0
	
	}//this is for the if sprout
		
	
	pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	 //cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   TheVessel->pLastVesselCell = pCurrentVesselCell2;
   //QuickPlot();// COMBINE
   cout <<"GrowSprout once"<<endl; 
   if(growstill==3)
   {
   
   pCurrentVesselPoint1 = pFirstVesselPoint;
   
   
   
   while(pCurrentVesselPoint1)
	{
		 pCurrentVesselPoint1->i_origin = pCurrentVesselPoint1->i;
		 pCurrentVesselPoint1->j_origin = pCurrentVesselPoint1->j;
		
	
			  
	  pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	}

   
   
   
   setorigin = 1;
   
   
   
   
   cout <<"origin reset"<<endl; 
   
   }//setorigin 
   
   
   
   }//still loop
   cout <<"function GrowSprout finish"<<endl;
 }
	
 
void VesselStructure:: Proliferate()
{
	//cout <<"function UpdateConductivity running"<<endl;
	extern Matrix ConductX, ConductY;
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix VisX, VisY;
	extern int Xnodes,Ynodes;
	extern Matrix VesselCells;
	extern Matrix SproutTips;
	extern Matrix VEGFR;
	extern Matrix Notch;
	extern Matrix Delta4;
	double pi = 3.141592654;
	
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	double RandNumber1 = 0.0;
	double RandNumber2 = 0.0;
	double k_v_proliferate =  RetrieveDoubleParameter( ParameterFile , "k_v_proliferate");
	double k_v_transit =  RetrieveDoubleParameter( ParameterFile , "k_v_transit");
	double min_vr =  RetrieveDoubleParameter( ParameterFile , "min_vr");
	double k_n_d_transit =  RetrieveDoubleParameter( ParameterFile , "k_n_d_transit");
	double min_n =  RetrieveDoubleParameter( ParameterFile , "min_n");
	double k_n_d_proliferate =  RetrieveDoubleParameter( ParameterFile , "k_n_d_proliferate");
	double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
	double r = 0;
	double N = 0;

	Matrix Prob_P = zeros(Xnodes, Ynodes);
	Matrix Prob_D = zeros(Xnodes, Ynodes);
	
	while(pCurrentVesselPoint)
	{
		int i = pCurrentVesselPoint->i;
		int j = pCurrentVesselPoint->j;
		if((pCurrentVesselPoint->MyAge) < VesselAgeInf)
		{
		N = 1;//*VesselCells(i,j);
		for(int k=0; k< N; k++)
		{
		
	
		*Prob_P(i,j) = Prob_Proliferate(i,j, k_v_proliferate, k_n_d_proliferate);
		
		RandNumber1 = float( rand() ) / float( RAND_MAX);
		if(RandNumber1<*Prob_P(i,j))
		{
			*VesselCells(i,j) += 1;
			CellNumber +=1;
		}//proliferate
					
					
		*Prob_D(i,j) = Prob_Transit(i, j, k_v_transit, k_n_d_transit);
			RandNumber2 = float( rand() ) / float( RAND_MAX);
			if(RandNumber2<*Prob_D(i,j))
			{
			*SproutTips(i,j) += 1;
			SproutNumber +=1;
			*VesselCells(i,j) -= 1;
			CellNumber -=1;
			}//tipcell
			
			
		
		}//proliferate and transit candidate
		
		}//if young
		
		
		
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
			
	}
	 /***********Normalize************/

  
 

	//cout <<"function UpdateConductivity finished"<<endl;
}
 
double Prob_Proliferate(int i, int j, double k_v_proliferate, double k_n_d_proliferate)
{
extern Matrix TAF;
extern Matrix VEGFR;
extern Matrix Notch;
extern Matrix Delta4;
double output = 0;

output = *TAF(i,j)/(1+k_n_d_proliferate* *Notch(i,j));

//output =( k_v_proliferate*(*TAF(i,j))/(1+*TAF(i,j)/ *VEGFR(i,j))-k_n_d_proliferate * *Delta4(i,j) * *Notch(i,j)/(*Delta4(i,j)+*Notch(i,j)))/(k_v_proliferate* *VEGFR(i,j));
if(output<0)
{
output = 0;
}
return output;
}

double Prob_Differentiate(int i, int j, double k_n_d_transit, double min_n)
{
extern Matrix TAF;
extern Matrix Notch;
extern Matrix Delta4;
extern Matrix SproutTips;
double output = 0;
output = 0;//1-1/(1+k_n_d_transit*min_n);//mightchange
if(output<0)
{
output = 0;
}
return output;

}

double Prob_Transit(int i, int j, double k_v_transit, double k_n_d_transit)
{
extern Matrix TAF;
extern Matrix Notch;
extern Matrix Delta4;
extern Matrix SproutTips;
extern Matrix VEGFR;
double output = 0;
//output = (k_v_transit*(*TAF(i,j))/(1+*TAF(i,j)/ *VEGFR(i,j))-k_n_d_transit* *Delta4(i,j) * *Notch(i,j)/(*Delta4(i,j)+*Notch(i,j)))/(k_v_transit* *VEGFR(i,j));
output = *TAF(i,j)/(1+k_n_d_transit * *Delta4(i,j) * *Notch(i,j));

if(output<0)
{
output = 0;
}
return output;

}

void VesselStructure:: CellDiffuse()
{
	//cout <<"function UpdateConductivity running"<<endl;
	extern Matrix ConductX, ConductY;
	extern Matrix BloodVesselRadiusXdir;
	extern Matrix BloodVesselRadiusYdir;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix VisX, VisY;
	extern int Xnodes,Ynodes;
	extern Matrix VesselCells;
	extern Matrix SproutTips;
	extern Matrix VEGFR;
	double pi = 3.141592654;
	
	VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
	double RandNumber1 = 0.0;
	double RandNumber2 = 0.0;
	
	bool DONE = 0;
	

	
	while(pCurrentVesselPoint)
	{
		int i = pCurrentVesselPoint->i;
		int j = pCurrentVesselPoint->j;
		DONE = 0;
		
				
		
		/**		
		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5&&!DONE)
		{
		
		if(*VesselCells(i+1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i+1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		else if(*VesselCells(i-1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i-1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		
		}//both connected 
		
		if(*BloodVesselIndicatorX(i,j)>0.5&&!DONE)
		{
		if(*VesselCells(i+1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i+1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		}// right
		
		if(*BloodVesselIndicatorX(i,j)<-0.5&&!DONE)
		{
		
		if(*VesselCells(i-1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i-1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		
		}//left
		
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5&&!DONE)
		{
		if(*VesselCells(i,j+1)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i,j+1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		else if(*VesselCells(i,j-1)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i,j-1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}

		}//both connected Y
		
		if(*BloodVesselIndicatorY(i,j)>0.5&&!DONE)
		{
		if(*VesselCells(i,j+1)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i,j+1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		}// TOP
		
		if(*BloodVesselIndicatorY(i,j)<-0.5&&!DONE)
		{
		if(*VesselCells(i,j-1) -*VesselCells(i,j)>= 2)
		{
		*VesselCells(i,j-1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		}//BOTTOM
		
		
		*/
		if(*SproutTips(i,j)>0)
		{
		
			if(min_abs(*BloodVesselIndicatorX(i,j))>1.5&&!DONE)
		{
		
		if(*VesselCells(i+1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i+1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		else if(*VesselCells(i-1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i-1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		
		}//both connected 
		
		if(*BloodVesselIndicatorX(i,j)>0.5&&!DONE)
		{
		if(*VesselCells(i+1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i+1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		}// right
		
		if(*BloodVesselIndicatorX(i,j)<-0.5&&!DONE)
		{
		
		if(*VesselCells(i-1,j)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i-1,j)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		
		}//left
		
		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5&&!DONE)
		{
		if(*VesselCells(i,j+1)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i,j+1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		else if(*VesselCells(i,j-1)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i,j-1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}

		}//both connected Y
		
		if(*BloodVesselIndicatorY(i,j)>0.5&&!DONE)
		{
		if(*VesselCells(i,j+1)-*VesselCells(i,j) >= 2)
		{
		*VesselCells(i,j+1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		}// TOP
		
		if(*BloodVesselIndicatorY(i,j)<-0.5&&!DONE)
		{
		if(*VesselCells(i,j-1) -*VesselCells(i,j)>= 2)
		{
		*VesselCells(i,j-1)-=1;
		*VesselCells(i,j)+=1;
		DONE = 1;
		}
		}//BOTTOM
		
		}//if a sprout
		
		pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;
			
	}
	 /***********Normalize************/

  
 

	//cout <<"function UpdateConductivity finished"<<endl;
}

void VesselStructure::UpdateVEGFR()
{
cout <<"function UpdateVEGFR running"<<endl;
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix SproutTips;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadius;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY, BloodVesselIndicator;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 extern Matrix TAF;
 extern Matrix VesselCells;
 extern Matrix VEGFR;
 extern Matrix SproutTips;
 
double min_vr = RetrieveDoubleParameter( ParameterFile , "min_vr");
  double min_C = RetrieveDoubleParameter( ParameterFile , "min_C");
 //extern Matrix DirectionX, DirectionY;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 
    int i, j;

 VEGFR = zeros(Xnodes,Ynodes);
 


 
 while( pCurrentVesselPoint)
 {
 
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;
	*VEGFR(i,j) = min_vr;

 
pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
  cout <<"function UpdateVEGFR finished"<<endl;

}

void VesselStructure::UpdateNotch()
{
cout <<"function UpdateNotch running"<<endl;
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix SproutTips;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadius;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY, BloodVesselIndicator;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 extern Matrix TAF;
 extern Matrix VesselCells;
 extern Matrix VEGFR;
 extern Matrix SproutTips;
 extern Matrix Notch;
 

  double min_n = RetrieveDoubleParameter( ParameterFile , "min_n");
 //extern Matrix DirectionX, DirectionY;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 
    int i, j;

 Notch = zeros(Xnodes,Ynodes);
 


 
 while( pCurrentVesselPoint)
 {
 
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;
	*Notch(i,j) = min_n;

 
pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
  cout <<"function UpdateNotch finished"<<endl;

}


void VesselStructure::UpdateDelta4()
{
cout <<"function UpdateDelta4 running"<<endl;
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 int i, j, i_minus, j_minus, i_plus, j_plus;
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix SproutTips;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadius;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY, BloodVesselIndicator;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 extern Matrix TAF;
 extern Matrix VesselCells;
 extern Matrix VEGFR;
 extern Matrix SproutTips;
 extern Matrix Delta4;

  double k_v_dll = RetrieveDoubleParameter( ParameterFile , "k_v_dll");
 //extern Matrix DirectionX, DirectionY;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;


 Delta4 = zeros(Xnodes,Ynodes);
 


 
 while( pCurrentVesselPoint)
 {
 
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;
	
	
	double N = *SproutTips(i,j)+*VesselCells(i,j)-1;
	if(N>0)
	{
	*Delta4(i,j) = N*k_v_dll*(*TAF(i,j));//N*(k_v_dll*(*TAF(i,j))/(1+*TAF(i,j)/ *VEGFR(i,j)));
	}
	
if(j == Ynodes-1)// boundary condition again
		{
			j_plus = Ynodes-1;
			j_minus=j-1;
		}
		else if( j == 0)
		{
			j_minus =0;
			j_plus=j+1;
		}
		else
		{
			j_plus = j+1;
			j_minus = j-1;
		}
		
		if(i == Xnodes-1)// boundary condition again NOTES
		{
			i_plus = Xnodes-1;
			i_minus= i-1;
		}
		
		else if( i == 0)
		{
			i_minus =0;
			i_plus = i+1;
		}
		else
		{
			i_plus = i+1;
			i_minus = i-1;
		}
		
		

	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)<-0.5))//This means, the flow is come from upstream, not only go downstream. This enable us to add diluting in the system.
	{       
			
			*Delta4(i,j) += k_v_dll*(*TAF(i_minus,j));//*(*TAF(i_minus,j))/(1+*TAF(i_minus,j)/ *VEGFR(i_minus,j));
						
	}
	
	if((min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5))
	{		
	
			*Delta4(i,j) += k_v_dll*(*TAF(i_plus,j));//*(*TAF(i_plus,j))/(1+*TAF(i_plus,j)/ *VEGFR(i_plus,j));										
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)<-0.5))
	{
				
	        *Delta4(i,j) += k_v_dll*(*TAF(i,j_minus));//*(*TAF(i,j_minus))/(1+*TAF(i,j_minus)/ *VEGFR(i,j_minus));	
	}
	
	if((min_abs(*BloodVesselIndicatorY(i,j))>1.5||*BloodVesselIndicatorY(i,j)>0.5))
	{
		    *Delta4(i,j) += k_v_dll*(*TAF(i,j_plus));//*(*TAF(i,j_plus))/(1+*TAF(i,j_plus)/ *VEGFR(i,j_plus));						
	}
	

 // cout<<"BVR"<<*BloodVesselRadius(i,j);



 
pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
  cout <<"function UpdateDelta4 finished"<<endl;

}





 void VesselStructure::UpdatePermeability()
{
cout <<"function Permeability running"<<endl;
		extern char* ParameterFile;
	//-----------------------------------------------------------------------------------
 extern int Xnodes, Ynodes; 
 int i, j, i_minus, j_minus, i_plus, j_plus;
 extern Matrix WssX, WssY;
 extern Matrix FlowX, FlowY;
 extern Matrix SproutTips;
 extern Matrix HaematocritLevel;
 extern Matrix VisX, VisY; //viscosity
 extern Matrix BloodVesselRadius;
 extern Matrix BloodVesselRadiusXdir;
 extern Matrix BloodVesselRadiusYdir;
 extern Matrix BloodVesselIndicatorX, BloodVesselIndicatorY, BloodVesselIndicator;
 extern Matrix BloodNodePressure;
 extern Matrix VesselAge;
 extern Matrix TAF;
 extern Matrix VesselCells;
 extern Matrix VEGFR;
 extern Matrix SproutTips;
 extern Matrix Delta4;
 extern Matrix Permeability;
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
double Perm = RetrieveDoubleParameter( ParameterFile , "Perm");

  double k_v_dll = RetrieveDoubleParameter( ParameterFile , "k_v_dll");
 //extern Matrix DirectionX, DirectionY;
 VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;



 for(int ii=0; ii<Xnodes; ii++)
 {
	for(int jj=0; jj<Ynodes; jj++)
	{
	//cout<<*BloodVesselIndicatorX(ii,jj)<<endl;
	*Permeability(ii,jj)=0;//0.000001;
	//*Permeability(ii,jj)=0.000001;
	}
 }
 


 
 while( pCurrentVesselPoint)
 {
 
	i = pCurrentVesselPoint-> i;
	j = pCurrentVesselPoint-> j;
	
	if(*VesselAge(i,j)>=VesselAgeInf)
	{
	*Permeability(i,j)=0;//0.000001* *TAF(i,j);	
    }
	else
	{
	*Permeability(i,j)=Perm* *TAF(i,j);
	}

	


 
pCurrentVesselPoint = pCurrentVesselPoint-> pNextVesselPoint;
 	
 }
  cout <<"function Permeability finished"<<endl;

}

void VesselStructure::WSSTAFBranchByProbInterval_N(void)// We can develop others by physiological observation too,WAIT UNTIL 3 D,,,,THERE ARE SO MANY PROBLEM!!OR...I INVENT SOME CRITERIA
{
	//cout <<"function WSSTAFBranchByProbInterval running"<<endl;

extern Matrix WssX, WssY, Wss;
double SproutAgeThreshold = RetrieveDoubleParameter( ParameterFile , "SproutAgeThreshold");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
double VesselAgeSup = RetrieveDoubleParameter( ParameterFile , "VesselAgeSup");
extern int Xnodes, Ynodes;
extern Matrix TAF;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix Shuffle;

if( MaxWss < 1e-5 )
	{ MaxWss = 2.0; }
double RandNumber;
double ProbBranch;
int RandBranchDirection;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint;

 
 ofstream myfile13;
  myfile13.open ("debug.txt");
 
  
//cout<<MaxWss;
//getchar();
 

while ( pCurrentVesselPoint1 )
{
 int i = pCurrentVesselPoint1-> i;
 int j = pCurrentVesselPoint1-> j;
 *VesselAge(i, j) = pCurrentVesselPoint1-> MyAge;
 //cout<<"vesselage"<< *VesselAge(i, j);
 /*  cout << "else if: " << (i>1) << ", " << i << endl;
  cout << "else if: " << (i<Xnodes-2) << endl;
  cout << "else if: " << (*SproutTips(i, j) < 1.0e-5) << ", " << *SproutTips(i,j) << endl;
  cout << "else if: " << (*VesselAge(i, j) > VesselAgeInf) << ", " << *VesselAge(i,j) << ", " << VesselAgeInf << endl;
  cout << "else if: " << (*VesselAge(i, j) < VesselAgeSup) << ", " << VesselAgeSup << endl;
  cout << "if: " << (*SproutAge(i, j) > SproutAgeThreshold) << ", " << *SproutAge(i,j) << ", " << SproutAgeThreshold << endl;
 */
   if( (i>1)&&(i<Xnodes-2)&&(j>1)&&(j<Ynodes-2)&&*SproutTips(i, j) < 1e-5 && *VesselAge(i, j) >= VesselAgeInf && *VesselAge(i, j) < VesselAgeSup)
  {
	//cout<<"WssX   "<<*WssX(i,j)<<"TAF   "<<*TAF(i,j);
	if( *Wss(i, j) <= 0.2 * MaxWss )
	{
	ProbBranch = 0.0;
	}
	
	else if ( *Wss(i, j) > 0.2 * MaxWss && *Wss(i, j) <= 0.4 * MaxWss )
	{
	    if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.02;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.03;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.1;
		}
	}
	
	else if ( *Wss(i, j) > 0.4 * MaxWss && *Wss(i, j) <= 0.6 * MaxWss )
    {
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.2;
		}
	}
	
	else if ( *Wss(i, j) > 0.6 * MaxWss && *Wss(i, j) <= 0.8 * MaxWss )
    {
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.09;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.3;
		}
	}
  
	else if ( *Wss(i, j) > 0.8 * MaxWss )
	{
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.16;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.4;
		}
	} 
	
	
	;
	
	// X direction
	     RandNumber = float(rand())/float(RAND_MAX);
	if(RandNumber < ProbBranch && min_abs(*BloodVesselIndicator(i, j))<3.5 )
	{
	*SproutTips(i,j)=1;
	SproutNumber++;
	pCurrentVesselPoint1->MyAge = 0;
	}

  }


 
pCurrentVesselPoint1 = pCurrentVesselPoint1 -> pNextVesselPoint;

}
 myfile13.close();
//getchar();
//cout <<"function WSSTAFBranchByProbInterval finished"<<endl;
//extern Matrix DirectionY;

}

 void VesselStructure::WSSTAFBranchByProbInterval(Matrix& Shuffle)// We can develop others by physiological observation too,WAIT UNTIL 3 D,,,,THERE ARE SO MANY PROBLEM!!OR...I INVENT SOME CRITERIA
{
	cout <<"WSSTAFBRANCHfunctionrunning"<<endl;

extern Matrix WssX, WssY;
double SproutAgeThreshold = RetrieveDoubleParameter( ParameterFile , "SproutAgeThreshold");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
double VesselAgeSup = RetrieveDoubleParameter( ParameterFile , "VesselAgeSup");
double k_n_d_transit = RetrieveDoubleParameter( ParameterFile , "k_n_d_transit");


extern int Xnodes, Ynodes;
extern Matrix TAF;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern double t;

//if( MaxWss < 1e-5 )
	{ MaxWss = 1.0; }
double RandNumber;
double ProbBranch;
int RandBranchDirection;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint;
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint;
 
 ofstream myfile13;
  myfile13.open ("debug.txt");
 
  

 

while ( pCurrentVesselPoint1 )
{
 int i = pCurrentVesselPoint1-> i;
 int j = pCurrentVesselPoint1-> j;
 /*  cout << "else if: " << (i>1) << ", " << i << endl;
  cout << "else if: " << (i<Xnodes-2) << endl;
  cout << "else if: " << (*SproutTips(i, j) < 1.0e-5) << ", " << *SproutTips(i,j) << endl;
  cout << "else if: " << (*VesselAge(i, j) > VesselAgeInf) << ", " << *VesselAge(i,j) << ", " << VesselAgeInf << endl;
  cout << "else if: " << (*VesselAge(i, j) < VesselAgeSup) << ", " << VesselAgeSup << endl;
  cout << "if: " << (*SproutAge(i, j) > SproutAgeThreshold) << ", " << *SproutAge(i,j) << ", " << SproutAgeThreshold << endl;
 */
// if(t>=0.05)
// {
//cout<<" age  "<<*VesselAge(i,j);
// getchar();
// }
   if( (i>1)&&(i<Xnodes-2)&&*SproutTips(i, j) < 1e-5&&*VesselAge(i, j)>0.6&&*VesselAge(i,j)<0.6)
  {
		//cout << "I am wrong" << endl;
       
			ProbBranch = *TAF(i,j)/(k_n_d_transit* *TAF(i,j)+MaxTAF);
		
		
		 RandNumber = float(rand())/float(RAND_MAX);
         RandBranchDirection = 1+int(2*float(rand())/float(RAND_MAX));
		if( RandNumber < ProbBranch )
		{
		  int Grow = 0;
		if( *BloodVesselIndicatorX(i, j) < 0)
		   {
				int I = i + 1;
				int J = j;
		  if( (*BloodVesselIndicator(I, J)) < 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
			if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

		    
			    }
			
		
		if( RandBranchDirection == 1)
		{
		  I = i;
		  J = j + 1;
		   if(*BloodVesselIndicator(I, J)< 10e-16)//&&*Shuffle(I, J)<0.5
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionY(I, J) = 1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));		
		}  
		}
		else if ( RandBranchDirection == 2)
		{
		  I = i;
		  J = j - 1;
		if( *BloodVesselIndicator(I,J)< 10e-16)//&&*Shuffle(I, J)<0.5
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = -1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
			}
		
		  }
		
		}
		else if( *BloodVesselIndicatorX(i, j) > 0)
		{
		int I = i - 1;
		int J = j;
		
		  if( *BloodVesselIndicator(I, J) < 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		    //*DirectionX(I, J) = -1;
				if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		//}  
		if( RandBranchDirection == 1)
		{
		  I = i;
		  J = j - 1;
		if( *BloodVesselIndicator(I, J) < 10e-16)//&&*Shuffle(I, J)<0.5
		{   Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		 //   *SproutAge(i, j) = 0;
		  //  *DirectionY(I, J) = -1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

		} 
		}
		else if ( RandBranchDirection == 2)
		{
		  I = i;
		  J = j + 1;
		if( *BloodVesselIndicator(I, J)< 10e-16)//
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = 1;

		 		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));		} 
		
		}
		}
		
		else if( *BloodVesselIndicatorY(i, j) < 0)
		   {
				int I = i ;
				int J = j + 1;
		   if( *BloodVesselIndicator(I, J) < 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = 1;
		  		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));			    }
	
		
		if( RandBranchDirection == 1)
		{
		  I = i + 1;
		  J = j ;
          if( *BloodVesselIndicator(I, J) < 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
		  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		}
 
		else if ( RandBranchDirection == 2)
		{
		  I = i - 1;
		  J = j ;
		 if( *BloodVesselIndicator(I, J) < 10e-16)//
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionX(I, J) = -1;
		  		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));			    }
		
		    }
		}
		
		else if( *BloodVesselIndicatorY(i, j) > 0)
		{
		int I = i ;
		int J = j - 1;
		  if( *BloodVesselIndicator(I, J) < 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		    //*DirectionY(I, J) = -1;
	if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		

		//}  
		if( RandBranchDirection == 1)
		{
		  I = i - 1;
		  J = j;
	    if(*BloodVesselIndicator(I, J)< 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		  //  *DirectionX(I, J) = -1;
		   		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J)); 
			    }

		}  
		else if ( RandBranchDirection == 2)
		{
		  I = i + 1;
		  J = j;
		 if( *BloodVesselIndicator(I, J)< 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
		 if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

			    }
		}
  
  }//DIRECTIONY END
  
  if(Grow > 1)
  {
  SproutNumber++;
  }
  
  
  }//IF PROB END
  
  
  }//SPROUT END
 
  //else if( (i>1)&&(i<Xnodes-2)&&(j>1)&&(j<200)&&*SproutTips(i, j) < 1e-5 && *VesselAge(i, j) > VesselAgeInf && *VesselAge(i, j) < VesselAgeSup)

  else if( (i>1)&&(i<Xnodes-2)&&*SproutTips(i, j) < 1e-5 && *VesselAge(i, j) >=VesselAgeInf && *VesselAge(i, j) < VesselAgeSup)
  {
	if( *WssX(i, j) <= 0.2 * MaxWss )
	{
	ProbBranch = 0.0;
	}
	
	else if ( *WssX(i, j) > 0.2 * MaxWss && *WssX(i, j) <= 0.4 * MaxWss )
	{
	    if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.02;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.03;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.1;
		}
	}
	
	else if ( *WssX(i, j) > 0.4 * MaxWss && *WssX(i, j) <= 0.6 * MaxWss )
    {
		if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.2;
		}
	}
	
	else if ( *WssX(i, j) > 0.6 * MaxWss && *WssX(i, j) <= 0.8 * MaxWss )
    {
		if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.09;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.3;
		}
	}
  
	else if ( *WssX(i, j) > 0.8 * MaxWss )
	{
		if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.16;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.4;
		}
	} 
	
	if(*WssX(i,j)>=1)
	{
	ProbBranch = *TAF(i,j)/(k_n_d_transit* *TAF(i,j)+MaxTAF);
	}
	else
	{
	ProbBranch =*WssX(i,j)* *TAF(i,j)/(k_n_d_transit* *TAF(i,j)+MaxTAF);

	}
	// X direction
	     RandNumber = float(rand())/float(RAND_MAX);
        RandBranchDirection = 1+int(2*float(rand())/float(RAND_MAX));
		
		//if(j==Ynodes-1)
		//{
		//cout << "********* 2 **********" << endl;
		//getchar();
		//myfile13 <<MaxWss<<" MaxWss "<<*WssX(i,j)<<" Wss "<<MaxTAF<<" MaxTAF "<<*TAF(i,j)<<" TAF "<<ProbBranch<<" ProbBranch "<<RandNumber<<" RandNumber "<<RandBranchDirection<<" RandBranchDirection "<<endl;
		//}
	
	if(RandNumber < ProbBranch && min_abs(*BloodVesselIndicatorY(i, j))<1.5 )
	{
	 int Grow = 0;
	if(RandBranchDirection == 1&&j<(Ynodes-1))
	   {
	      int I = i;
		  int J = j + 1;
		   if(min_abs(*BloodVesselIndicator(I, J))<10e-5)//&&*Shuffle(I, J)<0.5
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionY(I, J) = 1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		      {
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		      }
		else
			{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
			    {*BloodVesselIndicatorY(i,j)=2;}
		else
			    {*BloodVesselIndicatorY(i,j)=1;}
			}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
				{
		         *BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
				}
		else
				{
		if(*BloodVesselIndicatorY(I,J)>0.5)
				{*BloodVesselIndicatorY(I,J)=2;}
		else
				{*BloodVesselIndicatorY(I,J)=-1;}
				}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));	
			}	
	
	
		}
	else if( RandBranchDirection == 2&&j>0 )
	{
	int I = i;
	int J = j - 1;
	
	if( *BloodVesselIndicator(I,J)< 10e-16)//&&*Shuffle(I, J)<0.5
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = -1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
		}
		
	  }
	if(Grow > 0)
  {
  SproutNumber++;
  }

	
   }
	
	
	
		 
	
	// Y Direction
	
	
	if( *WssY(i, j) <= 0.1 * MaxWss )//0.2
	{
	ProbBranch = 0.0;
	}
	
	else if ( *WssY(i, j) > 0.1 * MaxWss && *WssY(i, j) <= 0.2 * MaxWss )//0.2 0.4
	{
	    if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.02;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.03;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.1;
		}
	}
	
	else if ( *WssY(i, j) > 0.2 * MaxWss && *WssY(i, j) <= 0.4 * MaxWss )//0.4 0.6
    {
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.2;
		}
	}
	
	else if ( *WssY(i, j) > 0.4 * MaxWss && *WssY(i, j) <= 0.6 * MaxWss )//0.6 0.8
    {
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.09;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.3;
		}
	}
  
	else if ( *WssY(i, j) > 0.6 * MaxWss )//0.8
	{
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.16;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.4;
		}
	} 
	
	
	if(*WssY(i,j)>=1)
	{
	ProbBranch = *TAF(i,j)/(k_n_d_transit* *TAF(i,j)+MaxTAF);
	}
	else
	{
	ProbBranch =*WssY(i,j)* *TAF(i,j)/(k_n_d_transit* *TAF(i,j)+MaxTAF);

	}
	     RandNumber = float(rand())/float(RAND_MAX);
         RandBranchDirection = 1+int(2*float(rand())/float(RAND_MAX));
		 
		  if(RandNumber < ProbBranch && min_abs(*BloodVesselIndicatorX(i, j))<1.5)
		 {
		 int Grow = 0;
		 if(RandBranchDirection == 1 )
		 {
		 int I = i + 1;
		 int J = j;
		
		if( min_abs(*BloodVesselIndicator(I, J))< 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
		  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		}
		 else if( RandBranchDirection == 2)
		 {
		 int I = i - 1;
		 int J = j;
      if( min_abs(*BloodVesselIndicator(I, J)) < 10e-16)//&&*Shuffle(I, J)<0.5
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionX(I, J) = -1;
		  		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));			   
		 }
		
		}
	if(Grow > 0)
  {
  SproutNumber++;
  }
	}


  }


 
pCurrentVesselPoint1 = pCurrentVesselPoint1 -> pNextVesselPoint;

}
 myfile13.close();

pLastVesselPoint = pCurrentVesselPoint2;
cout <<"function WSSTAFBranchByProbInterval finished"<<endl;
//extern Matrix DirectionY;

}

void VesselStructure::WSSTAFBranchByProbInterval_R(void)// We can develop others by physiological observation too,WAIT UNTIL 3 D,,,,THERE ARE SO MANY PROBLEM!!OR...I INVENT SOME CRITERIA
{
	//cout <<"function WSSTAFBranchByProbInterval running"<<endl;

extern Matrix WssX, WssY;
double SproutAgeThreshold = RetrieveDoubleParameter( ParameterFile , "SproutAgeThreshold");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
double VesselAgeSup = RetrieveDoubleParameter( ParameterFile , "VesselAgeSup");
extern int Xnodes, Ynodes;
extern Matrix TAF;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix Shuffle;

//if( MaxWss < 1e-5 )
	{ MaxWss = 1.0; }
double RandNumber;
double ProbBranch;
int RandBranchDirection;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint;
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint;
 
 ofstream myfile13;
  myfile13.open ("debug.txt");
 
  
//cout<<MaxWss;
//getchar();
 

while ( pCurrentVesselPoint1 )
{
 int i = pCurrentVesselPoint1-> i;
 int j = pCurrentVesselPoint1-> j;
 *VesselAge(i, j) = pCurrentVesselPoint1-> MyAge;
 //cout<<"vesselage"<< *VesselAge(i, j);
 /*  cout << "else if: " << (i>1) << ", " << i << endl;
  cout << "else if: " << (i<Xnodes-2) << endl;
  cout << "else if: " << (*SproutTips(i, j) < 1.0e-5) << ", " << *SproutTips(i,j) << endl;
  cout << "else if: " << (*VesselAge(i, j) > VesselAgeInf) << ", " << *VesselAge(i,j) << ", " << VesselAgeInf << endl;
  cout << "else if: " << (*VesselAge(i, j) < VesselAgeSup) << ", " << VesselAgeSup << endl;
  cout << "if: " << (*SproutAge(i, j) > SproutAgeThreshold) << ", " << *SproutAge(i,j) << ", " << SproutAgeThreshold << endl;
 */
 
   if( *SproutTips(i, j) > 1e-5 && *SproutAge(i, j) > SproutAgeThreshold&&1==0)
  {
		
	//	cout << "I am wrong" << endl;
        if(*TAF(i, j) <= 0.4 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.4 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.2;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.3;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.4;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 1.0;
		}
		
		 RandNumber = float(rand())/float(RAND_MAX);
         RandBranchDirection = 1+int(2*float(rand())/float(RAND_MAX));
		if( RandNumber < ProbBranch )
		{
		  int Grow = 0;
		if( *BloodVesselIndicatorX(i, j) < 0)
		   {
				int I = i + 1;
				int J = j;
		  if( (*BloodVesselIndicator(I, J)) < 10e-16&&*Shuffle(I, J)<0.5)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
			if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

		    
			    }
			
		
		if( RandBranchDirection == 1)
		{
		  I = i;
		  J = j + 1;
		   if(*BloodVesselIndicator(I, J)< 10e-16&&*Shuffle(I, J)<0.5)
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionY(I, J) = 1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));		
		}  
		}
		else if ( RandBranchDirection == 2)
		{
		  I = i;
		  J = j - 1;
		if( *BloodVesselIndicator(I,J)< 10e-16&&*Shuffle(I, J)<0.5)
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = -1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
			}
		
		  }
		
		}
		else if( *BloodVesselIndicatorX(i, j) > 0)
		{
		int I = i - 1;
		int J = j;
		
		  if( *BloodVesselIndicator(I, J) < 10e-16&&*Shuffle(I, J)<0.5)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		    //*DirectionX(I, J) = -1;
				if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		//}  
		if( RandBranchDirection == 1)
		{
		  I = i;
		  J = j - 1;
		if( *BloodVesselIndicator(I, J) < 10e-16&&*Shuffle(I, J)<0.5)
		{   Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		 //   *SproutAge(i, j) = 0;
		  //  *DirectionY(I, J) = -1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

		} 
		}
		else if ( RandBranchDirection == 2)
		{
		  I = i;
		  J = j + 1;
		if( *BloodVesselIndicator(I, J)< 10e-16&&*Shuffle(I, J)<0.5)
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = 1;

		 		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));		} 
		
		}
		}
		
		else if( *BloodVesselIndicatorY(i, j) < 0)
		   {
				int I = i ;
				int J = j + 1;
		   if( *BloodVesselIndicator(I, J) < 10e-16&&*Shuffle(I, J)<0.5)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = 1;
		  		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)>0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=-1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));			    }
	
		
		if( RandBranchDirection == 1)
		{
		  I = i + 1;
		  J = j ;
          if( *BloodVesselIndicator(I, J) < 10e-16&&*Shuffle(I, J)<0.5)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
		  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		}
 
		else if ( RandBranchDirection == 2)
		{
		  I = i - 1;
		  J = j ;
		 if( *BloodVesselIndicator(I, J) < 10e-16&&*Shuffle(I, J)<0.5)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionX(I, J) = -1;
		  		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));			    }
		
		    }
		}
		
		else if( *BloodVesselIndicatorY(i, j) > 0)
		{
		int I = i ;
		int J = j - 1;
		  if( *BloodVesselIndicator(I, J) < 10e-16&&*Shuffle(I, J)<0.5)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		    //*DirectionY(I, J) = -1;
	if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		

		//}  
		if( RandBranchDirection == 1)
		{
		  I = i - 1;
		  J = j;
	    if(*BloodVesselIndicator(I, J)< 10e-16&&*Shuffle(I, J)<0.5)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		  //  *DirectionX(I, J) = -1;
		   		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J)); 
			    }

		}  
		else if ( RandBranchDirection == 2)
		{
		  I = i + 1;
		  J = j;
		 if( *BloodVesselIndicator(I, J)< 10e-16&&*Shuffle(I, J)<0.5)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
		 if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

			    }
		}
  
  }//DIRECTIONY END
  
  if(Grow > 1)
  {
  SproutNumber++;
  }
  
  
  }//IF PROB END
  
  
  }//SPROUT END
 
  //else if( (i>1)&&(i<Xnodes-2)&&(j>1)&&(j<200)&&*SproutTips(i, j) < 1e-5 && *VesselAge(i, j) > VesselAgeInf && *VesselAge(i, j) < VesselAgeSup)

  else if( (i>1)&&(i<Xnodes-2)&&(j>1)&&(j<Ynodes-2)&&*SproutTips(i, j) < 1e-5 && *VesselAge(i, j) >= VesselAgeInf && *VesselAge(i, j) < VesselAgeSup)
  {
	//cout<<"WssX   "<<*WssX(i,j)<<"TAF   "<<*TAF(i,j);
	if( *WssX(i, j) <= 0.2 * MaxWss )
	{
	ProbBranch = 0.0;
	}
	
	else if ( *WssX(i, j) > 0.2 * MaxWss && *WssX(i, j) <= 0.4 * MaxWss )
	{
	    if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.02;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.03;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.1;
		}
	}
	
	else if ( *WssX(i, j) > 0.4 * MaxWss && *WssX(i, j) <= 0.6 * MaxWss )
    {
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.2;
		}
	}
	
	else if ( *WssX(i, j) > 0.6 * MaxWss && *WssX(i, j) <= 0.8 * MaxWss )
    {
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.09;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.3;
		}
	}
  
	else if ( *WssX(i, j) > 0.8 * MaxWss )
	{
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.6 * MaxTAF)
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.6 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF)
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF && *TAF(i, j) <= 0.9 * MaxTAF) 
		{
			ProbBranch = 0.16;
		}
		else if (*TAF(i, j) > 0.9 * MaxTAF)
		{
			ProbBranch = 0.4;
		}
	} 
	
	
	
	
	// X direction
	     RandNumber = float(rand())/float(RAND_MAX);
        RandBranchDirection = 1+int(2*float(rand())/float(RAND_MAX));
		
		//if(j==Ynodes-1)
		//{
		//cout << "********* 2 **********" << endl;
		//getchar();
		//myfile13 <<MaxWss<<" MaxWss "<<*WssX(i,j)<<" Wss "<<MaxTAF<<" MaxTAF "<<*TAF(i,j)<<" TAF "<<ProbBranch<<" ProbBranch "<<RandNumber<<" RandNumber "<<RandBranchDirection<<" RandBranchDirection "<<endl;
		//}
	
	if(RandNumber < ProbBranch && min_abs(*BloodVesselIndicatorY(i, j))<1.5 )
	{
	 int Grow = 0;
	if(RandBranchDirection == 1&&j<(Ynodes-1))
	   {
	      int I = i;
		  int J = j + 1;
		   if(min_abs(*BloodVesselIndicator(I, J))<10e-5&&*Shuffle(I, J)<0.5)
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionY(I, J) = 1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		      {
		*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
		      }
		else
			{
		if(*BloodVesselIndicatorY(i,j)<-0.5)
			    {*BloodVesselIndicatorY(i,j)=2;}
		else
			    {*BloodVesselIndicatorY(i,j)=1;}
			}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
				{
		         *BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
				}
		else
				{
		if(*BloodVesselIndicatorY(I,J)>0.5)
				{*BloodVesselIndicatorY(I,J)=2;}
		else
				{*BloodVesselIndicatorY(I,J)=-1;}
				}

		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));	
			}	
	
	
		}
	else if( RandBranchDirection == 2&&j>0 )
	{
	int I = i;
	int J = j - 1;
	
	if( *BloodVesselIndicator(I,J)< 10e-16&&*Shuffle(I, J)<0.5)
			{
			Grow++;
		    VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;

		
		    *SproutTips(i, j) = 0;
		    *SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; // *SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionY(I, J) = -1;

		if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
		{
		*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorY(i,j)>0.5)
		{*BloodVesselIndicatorY(i,j)=2;}
		else
		{*BloodVesselIndicatorY(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
		{
		*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorY(I,J)<-0.5)
		{*BloodVesselIndicatorY(I,J)=2;}
		else
		{*BloodVesselIndicatorY(I,J)=1;}
		}
		
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			
		}
		
	  }
	if(Grow > 0)
  {
  SproutNumber++;
  }

	
   }
	
	
	
		 
	
	// Y Direction
	//cout<<"WssY   "<<*WssY(i,j)<<"TAF   "<<*TAF(i,j);
	
	if( *WssY(i, j) <= 0.1 * MaxWss )//0.2
	{
	ProbBranch = 0.0;
	}
	
	else if ( *WssY(i, j) > 0.1 * MaxWss && *WssY(i, j) <= 0.2 * MaxWss )//0.2 0.4
	{
	    if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.02;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.03;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.1;
		}
	}
	
	else if ( *WssY(i, j) > 0.2 * MaxWss && *WssY(i, j) <= 0.4 * MaxWss )//0.4 0.6
    {
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.04;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.2;
		}
	}
	
	else if ( *WssY(i, j) > 0.4 * MaxWss && *WssY(i, j) <= 0.6 * MaxWss )//0.6 0.8
    {
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.06;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.09;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.3;
		}
	}
  
	else if ( *WssY(i, j) > 0.6 * MaxWss )//0.8
	{
		if(*TAF(i, j) <= 0.3 * MaxTAF)
		{ 
			ProbBranch = 0.0;
		}
		else if (*TAF(i, j) > 0.3 * MaxTAF && *TAF(i, j) <= 0.5 * MaxTAF)
		{
			ProbBranch = 0.08;
		}
		else if (*TAF(i, j) > 0.5 * MaxTAF && *TAF(i, j) <= 0.7 * MaxTAF)
		{
			ProbBranch = 0.12;
		}
		else if (*TAF(i, j) > 0.7 * MaxTAF && *TAF(i, j) <= 0.8 * MaxTAF) 
		{
			ProbBranch = 0.16;
		}
		else if (*TAF(i, j) > 0.8 * MaxTAF)
		{
			ProbBranch = 0.4;
		}
	} 
	
	     RandNumber = float(rand())/float(RAND_MAX);
         RandBranchDirection = 1+int(2*float(rand())/float(RAND_MAX));
		 
		  if(RandNumber < ProbBranch && min_abs(*BloodVesselIndicatorX(i, j))<1.5)
		 {
		 int Grow = 0;
		 if(RandBranchDirection == 1 )
		 {
		 int I = i + 1;
		 int J = j;
		
		if( min_abs(*BloodVesselIndicator(I, J))< 10e-16&&*Shuffle(I, J)<0.5)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		   // *DirectionX(I, J) = 1;
		  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)<-0.5)
		    {*BloodVesselIndicatorX(i,j)=2;}
		else
			{*BloodVesselIndicatorX(i,j)=1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)>0.5)
			{*BloodVesselIndicatorX(I,J)=2;}
		else
			{*BloodVesselIndicatorX(I,J)=-1;}
		}
		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			    }
		}
		 else if( RandBranchDirection == 2)
		 {
		 int I = i - 1;
		 int J = j;
      if( min_abs(*BloodVesselIndicator(I, J)) < 10e-16&&*Shuffle(I, J)<0.5)
				{
		Grow++;
		VesselPoint* pNewVesselPoint = new VesselPoint;
	    pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
	    pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
		pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
		pCurrentVesselPoint2->i = I;
		pCurrentVesselPoint2->j = J;
		pCurrentVesselPoint2->pNextVesselPoint = NULL;
		VesselListLength++;
		
			*SproutTips(i, j) = 0;
			*SproutTips(I, J) = 1;
		    *SproutAge(I, J) = 0; //*SproutAge(i, j) + AngioDeltaT;
		    *SproutAge(i, j) = 0;
		 //   *DirectionX(I, J) = -1;
		  		if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
		{
		*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
		}
		else
		{
		if(*BloodVesselIndicatorX(i,j)>0.5)
		{*BloodVesselIndicatorX(i,j)=2;}
		else
		{*BloodVesselIndicatorX(i,j)=-1;}
		}
		if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
		{
		*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
		}
		else
		{
		if(*BloodVesselIndicatorX(I,J)<-0.5)
		{*BloodVesselIndicatorX(I,J)=2;}
		else
		{*BloodVesselIndicatorX(I,J)= 1;}
		}

		*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
		*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));			   
		 }
		
		}
	if(Grow > 0)
  {
  SproutNumber++;
  }
	}


  }


 
pCurrentVesselPoint1 = pCurrentVesselPoint1 -> pNextVesselPoint;

}
 myfile13.close();
//getchar();
pLastVesselPoint = pCurrentVesselPoint2;
//cout <<"function WSSTAFBranchByProbInterval finished"<<endl;
//extern Matrix DirectionY;

}


void VesselStructure::CleanUp( Matrix& Shuffle)
{
cout <<"function Cleanup running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix BloodNodePressure;
extern Matrix TAF;
extern Matrix ECM;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
//sweep the list to update rather than matrix?
//yes!
//should we have more list?
//we don't need to, because even if we have two main vessel,
//it's like a whole system with the general BC and IC.

//DON'T FORGET THE INDICATOR
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list

int LoopLength = VesselListLength; 

cout<<"Growth Begin"<<endl;
for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"enter loop"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
	
 // cout<<i<<"    "<<j<<endl;
	//if(*Shuffle(i,j)>0&&pCurrentVesselPoint1->AmIArtery==0)
	if(*BloodNodePressure(i,j)<1.0)
	{
		//	cout<<"beginkill";
				
	//tune the indicator matrix
	    if (i == 0) {iminus=i;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=j;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = i;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = j;} else  {jplus = j+1;}
	
	  int I, J;
	  
	  I=iplus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=1;}
	  else if(*BloodVesselIndicatorX(I,J)<-0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	
	  I=iminus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=-1;}
	  else if(*BloodVesselIndicatorX(I,J)>0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
	
	 
	  I=i;
	  J=jplus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=1;}
	  else if(*BloodVesselIndicatorY(I,J)<-0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	  I=i;
	  J=jminus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=-1;}
	  else if(*BloodVesselIndicatorY(I,J)>0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	  I=i;
	  J=j;
	  *BloodVesselIndicatorX(I,J)=0;
	  *BloodVesselIndicatorY(I,J)=0;	 	  
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
			  
				
				  
					
	//tune the chain
					  
					  
	VesselPoint* pTempVesselPoint = pCurrentVesselPoint1;
	cout<<"i:"<<i<<",j:"<<j<<"  ";
		touchpoints++;
		cout<<"touchpoints"<<touchpoints<<"  ";

	
	
	if(pCurrentVesselPoint1->pNextVesselPoint)
	{
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	else
	{
	cout<<"the last point"<<endl;
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=NULL;
	//(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint2= pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	 
	   pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	   
	   
	  pTempVesselPoint->pPreviousVesselPoint	= NULL;
	  pTempVesselPoint->pNextVesselPoint = NULL;
	  pTempVesselPoint->i = 0;
	  pTempVesselPoint->j = 0;
	  pTempVesselPoint->AmIArtery = 0;
	  delete pTempVesselPoint;

	 VesselListLength=VesselListLength-1;
	 
	 
	 if(*SproutTips(i,j)>0)
	 {
		*SproutTips(i,j)=0;
		*SproutAge(i,j)=0;
		SproutNumber=SproutNumber-1;
	 
	 }
	 else
	 {
	 *VesselAge(i,j)=0;
	 }
	 
	
	
	
	
	
	
	
	}//this is for the kill of points
	
	
		 //cout<<"beginmove";
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	 //cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   QuickPlot();// COMBINE
   cout <<"function Cleanup finished"<<endl; 
 }

void UpdateStability(void)
{
extern Matrix Stability;
extern Matrix BloodVesselIndicator;
extern Matrix TAF;
extern Matrix Wss;
extern int Xnodes,Ynodes;
double Stable_down = RetrieveDoubleParameter( ParameterFile , "Stable_down");
double Stable_up = RetrieveDoubleParameter( ParameterFile , "Stable_up");

for (int j = 0; j < Ynodes; j++)
 {
for(int i = 0; i < Xnodes; i++)
  {
       //if(*TAF(i,j) > 0) 
       // {
        //   *Stability(i,j) = *Stability(i,j) - AngioDeltaT * Stable_down * *TAF(i,j);
	//	}
	//	else
		if(*BloodVesselIndicator(i,j) > 0)
		{
		   //*Stability(i,j) = *Stability(i,j) + AngioDeltaT * Stable_up;
		
      if(*Stability(i,j) < 0)
        {
			*Stability(i,j) = 0;
		}
	  else if(*Stability(i,j)>1)
	    {
			*Stability(i,j) = 1;
	    }

		}
		else
		{
		*Stability(i,j) = 0;
		}
	
     }
 }


}


void VesselStructure::Retract(CellVesselStructure* TheVessel)
{
cout <<"function Cleanup running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
extern Matrix Stability;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselIndicator;
extern Matrix BloodNodePressure;
extern Matrix TAF;
extern Matrix ECM;
extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
//sweep the list to update rather than matrix?
//yes!
//should we have more list?
//we don't need to, because even if we have two main vessel,
//it's like a whole system with the general BC and IC.

//DON'T FORGET THE INDICATOR
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
Matrix Prob0 = zeros(Xnodes, Ynodes);
Matrix Prob1 = zeros(Xnodes, Ynodes);
Matrix Prob2 = zeros(Xnodes, Ynodes);
Matrix Prob3 = zeros(Xnodes, Ynodes);
Matrix Prob4 = zeros(Xnodes, Ynodes);
Matrix Inf0 = zeros(Xnodes, Ynodes);
Matrix Inf1 = zeros(Xnodes, Ynodes);
Matrix Inf2 = zeros(Xnodes, Ynodes);
Matrix Inf3 = zeros(Xnodes, Ynodes);
Matrix Inf4 = zeros(Xnodes, Ynodes);
Matrix Sup0 = zeros(Xnodes, Ynodes);
Matrix Sup1 = zeros(Xnodes, Ynodes);
Matrix Sup2 = zeros(Xnodes, Ynodes);
Matrix Sup3 = zeros(Xnodes, Ynodes);
Matrix Sup4 = zeros(Xnodes, Ynodes);
int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list

    
	VesselCell* pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
	VesselCell* pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to find out the current cell respond to the point

	VesselCell* pTempVesselCell = TheVessel->pFirstVesselCell;//This is to delete
extern Matrix Shuffle;
Shuffle = zeros(Xnodes, Ynodes);
for(int i = 0; i<Xnodes; i++)
{
for(int j = 0; j<Ynodes; j++)
{
if(*BloodVesselIndicator(i,j)==1&&*Stability(i,j)>0.5)
{
*Shuffle(i,j) = 1;
}


}
}


int LoopLength = VesselListLength; 


for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"give position"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
 //  cout<<i<<"    "<<j<<endl;
// cout<<*Shuffle(i,j);	
//cout<<"beginkill";

	//if(*Shuffle(i,j)>0&&pCurrentVesselPoint1->AmIArtery==0)
	if(*Shuffle(i,j) >0&&pCurrentVesselPoint1->AmIArtery==0&&pCurrentVesselPoint1->AmIVenous==0)
	{	
							
	//tune the indicator matrix
	    if (i == 0) {iminus=i;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=j;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = i;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = j;} else  {jplus = j+1;}
	
	  int I, J;
	  
	  I=iplus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=1;}
	  else if(*BloodVesselIndicatorX(I,J)<-0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	
	  I=iminus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=-1;}
	  else if(*BloodVesselIndicatorX(I,J)>0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
	
	 
	  I=i;
	  J=jplus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=1;}
	  else if(*BloodVesselIndicatorY(I,J)<-0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	  I=i;
	  J=jminus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=-1;}
	  else if(*BloodVesselIndicatorY(I,J)>0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	  I=i;
	  J=j;
	  *BloodVesselIndicatorX(I,J)=0;
	  *BloodVesselIndicatorY(I,J)=0;	 	  
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	
	

	
	
	
	

	
	
	
	 pTempVesselCell = TheVessel->FindCell(i,j);
	// cout<<pTempVesselCell->i[0]<<"  "<<pTempVesselCell->j[0]<<endl;
	  if(pTempVesselCell->cCover > 1)
	  {
	  
		RemoveUnConnectedPoint(pTempVesselCell, i, j);	
		
	  }
	  else if(pTempVesselCell->cCover == 1)
	  {
	pCurrentVesselCell1 = TheVessel->pFirstVesselCell; 
	pCurrentVesselCell1 = pCurrentVesselCell1->pNextVesselCell;

  while(pCurrentVesselCell1)
  {    
  if(Connected(pTempVesselCell,pCurrentVesselCell1))
  {
  cout<<"Before"<<endl;
    DisConnect(pTempVesselCell,pCurrentVesselCell1);
	 cout<<"After"<<endl;
  }
  
  pCurrentVesselCell1 = pCurrentVesselCell1->pNextVesselCell;
  }
    if(pTempVesselCell->pNextVesselCell)
	{
	(pTempVesselCell->pPreviousVesselCell)->pNextVesselCell=pTempVesselCell->pNextVesselCell;
	(pTempVesselCell->pNextVesselCell)->pPreviousVesselCell=pTempVesselCell->pPreviousVesselCell;
	}
	else
	{
	cout<<"the last point"<<endl;
	(pTempVesselCell->pPreviousVesselCell)->pNextVesselCell=NULL;
	//(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselCell2 = pTempVesselCell->pPreviousVesselCell;
	 
	}
	
	   
	   
	   
	  pTempVesselCell->pPreviousVesselCell	= NULL;
	  pTempVesselCell->pNextVesselCell = NULL;
	 
	  delete pTempVesselCell;

	 TheVessel->pLastVesselCell = pCurrentVesselCell2;
	  
	  }
				
			  
					
	//tune the chain
					  
					  
	VesselPoint* pTempVesselPoint = pCurrentVesselPoint1;
	cout<<"i:"<<i<<",j:"<<j<<"  ";
		touchpoints++;
		cout<<"touchpoints"<<touchpoints<<"  ";
cout<<"begin loop"<<endl;
	

	
	if(pCurrentVesselPoint1->pNextVesselPoint&&pCurrentVesselPoint1->pPreviousVesselPoint)
	{cout<<"enter loop1"<<endl;
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=pCurrentVesselPoint1->pNextVesselPoint;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	else if(pCurrentVesselPoint1->pNextVesselPoint)
	{
	cout<<"enter loop2"<<endl;
	(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=NULL;
	pFirstVesselPoint = pCurrentVesselPoint1->pNextVesselPoint;
	
	pCurrentVesselPoint1 = pFirstVesselPoint;
	}
	else
	{
	cout<<"enter loop3"<<endl;
	cout<<"the last point"<<endl;
	(pCurrentVesselPoint1->pPreviousVesselPoint)->pNextVesselPoint=NULL;
	//(pCurrentVesselPoint1->pNextVesselPoint)->pPreviousVesselPoint=pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint2= pCurrentVesselPoint1->pPreviousVesselPoint;
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pPreviousVesselPoint;
	}
	
 
	   
	 	 
	   	   
	   
	   
	   
	   
	   
	   
	  pTempVesselPoint->pPreviousVesselPoint	= NULL;
	  pTempVesselPoint->pNextVesselPoint = NULL;
	  pTempVesselPoint->i = 0;
	  pTempVesselPoint->j = 0;
	  pTempVesselPoint->AmIArtery = 0;
	  delete pTempVesselPoint;

	 VesselListLength=VesselListLength-1;
	 
	 
	 if(*SproutTips(i,j)>0)
	 {
		*SproutTips(i,j)=0;
		*SproutAge(i,j)=0;
		SproutNumber=SproutNumber-1;
	 
	 }
	 else
	 {
	 *VesselAge(i,j)=0;
	 }
	 
	
	cout<<"end loop"<<endl;

	
	if(pCurrentVesselPoint1 != pFirstVesselPoint)
		 {
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
		 }

	
	
	
	}//this is for the kill of points
	else
	{
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	}
	
		 //cout<<"beginmove";
		 
	// cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   QuickPlot();// COMBINE
   cout <<"function Cleanup finished"<<endl; 

}



void VesselStructure::MakeList(void)
{
extern Matrix BloodVesselIndicator;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
Matrix Used;
Matrix Index;
extern int Xnodes, Ynodes;
extern double t;
Used = zeros(Xnodes,Ynodes);
Index = zeros(Xnodes, Ynodes);
int index=1;
int I,J;

 extern char SaveDirectory[1025];
 char temp_string[1024];
 
 /*********generate index and save position*******/
 
 //sprintf( Command, "./CompositeFrame %f frame%08i.bmp", time, frame );
 
 
 sprintf( temp_string, "%s/position_list_%d.txt", SaveDirectory,t);
 ofstream position_list( temp_string, ios::out );
			  
  
  VesselPoint* pCurrentVesselPoint = pFirstVesselPoint;
 
 while(pCurrentVesselPoint)
 {
 I =  pCurrentVesselPoint->i;
 J =  pCurrentVesselPoint->j;
 *Index(I,J) = index;
 position_list  <<I<<"\t"
				<<J<<endl;
										
 pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;	
 index++;
								
 }
 
 
 position_list.close();	

 
 
 /*********generate branch list*****************/
 
 
 //save list now
  sprintf( temp_string , "%s/branch_list_%d.txt" , SaveDirectory,t );
 ofstream branch_list( temp_string, ios::out );
  pCurrentVesselPoint = pFirstVesselPoint;
 while(pCurrentVesselPoint)
 {
 I= pCurrentVesselPoint->i;
 J= pCurrentVesselPoint->j;

 if(*BloodVesselIndicator(I,J)>2)
 {
  *Used(I,J)=1;
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 I = pCurrentVesselPoint->i+1;
 J = pCurrentVesselPoint->j;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;
//again
I = pCurrentVesselPoint->i-1;
 J = pCurrentVesselPoint->j;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;



 
 
 
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1)
 {
  I = pCurrentVesselPoint->i+1;
 J = pCurrentVesselPoint->j;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;

 }
 else if(*BloodVesselIndicatorX(I,J)==-1)
 {
 I = pCurrentVesselPoint->i-1;
 J = pCurrentVesselPoint->j;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;

 }
 //Ydirection
  if(*BloodVesselIndicatorY(I,J)==2)
 {
 I = pCurrentVesselPoint->i;
 J = pCurrentVesselPoint->j+1;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;
//again
I = pCurrentVesselPoint->i;
J = pCurrentVesselPoint->j-1;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;



 
 
 
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1)
 {
  I = pCurrentVesselPoint->i;
 J = pCurrentVesselPoint->j+1;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;
 }
 else if(*BloodVesselIndicatorY(I,J)==-1)
 {
 I = pCurrentVesselPoint->i;
 J = pCurrentVesselPoint->j-1;
  
 while((*Used(I,J)==0)&&(*BloodVesselIndicator(I,J)==2))
 {
 *Used(I,J)=1;
 branch_list <<*Index(I,J)<<"\t";
 if(*BloodVesselIndicatorX(I,J)==2)
 {
 
 if(*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 
 }
 else if(*BloodVesselIndicatorX(I,J)==1&&*Used(I+1,J)==0)
 {
  I=I+1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorX(I,J)==-1&&*Used(I-1,J)==0)
 {
  I=I-1;
  J=J;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==2)
 {
 
 if(*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }
 
 }
 else if(*BloodVesselIndicatorY(I,J)==1&&*Used(I,J+1)==0)
 {
  I=I;
  J=J+1;
   
 }
 else if(*BloodVesselIndicatorY(I,J)==-1&&*Used(I,J-1)==0)
 {
  I=I;
  J=J-1;
   
 }




 }
branch_list <<endl;
branch_list <<endl;

 }
 
 
 }
 
 pCurrentVesselPoint = pCurrentVesselPoint->pNextVesselPoint;					
 }
 branch_list.close();
// cout <<"function SaveListInOrder finished"<<endl;



}
 
void VesselStructure::Remodel(CellVesselStructure* TheVessel)
{
cout <<"function Cleanup running"<<endl; 
extern bool VesselBroke;
extern int touchpoints;
extern Matrix SproutTips;
extern Matrix SproutAge;
extern Matrix VesselAge;
extern Matrix Stability;
//extern Matrix DirectionX;
//extern Matrix DirectionY;
extern Matrix BloodVesselIndicatorX;
extern Matrix BloodVesselIndicatorY;
extern Matrix BloodVesselPressureX;
extern Matrix BloodVesselPressureY;
extern Matrix WssX;
extern Matrix WssY;
extern Matrix BloodVesselIndicator;
extern Matrix BloodNodePressure;
extern Matrix TAF;
extern Matrix ECM;
 double Pin, Pout;
 double Ratio;
Pin = RetrieveDoubleParameter( ParameterFile , "Pin");
Pout = RetrieveDoubleParameter( ParameterFile , "Pout");

extern int Xnodes, Ynodes;
double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
double VesselAgeInf = RetrieveDoubleParameter( ParameterFile , "VesselAgeInf");
//sweep the list to update rather than matrix?
//yes!
//should we have more list?
//we don't need to, because even if we have two main vessel,
//it's like a whole system with the general BC and IC.

//DON'T FORGET THE INDICATOR
AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
AngioDeltaY = 1.0 /Ynodes;
int iminus,iplus,jminus,jplus;
VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list

    
	VesselCell* pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
	VesselCell* pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to find out the current cell respond to the point

	VesselCell* pTempVesselCell = TheVessel->pFirstVesselCell;//This is to delete


int LoopLength = VesselListLength; 


for(int Loop = 0; Loop < LoopLength; Loop++)
{
   //cout<<"give position"<<endl;
	int i = pCurrentVesselPoint1->i;
	int j = pCurrentVesselPoint1->j;
 //  cout<<i<<"    "<<j<<endl;
// cout<<*Shuffle(i,j);	
//cout<<"beginkill";

	//if(*Shuffle(i,j)>0&&pCurrentVesselPoint1->AmIArtery==0)
	if(*BloodVesselIndicator(i,j) > 2&&pCurrentVesselPoint1->AmIArtery==0&&pCurrentVesselPoint1->AmIVenous==0)
	{	
							
	//tune the indicator matrix
	    if (i == 0) {iminus=i;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
		if (j == 0) {jminus=j;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
		if (i == Xnodes-1) {iplus = i;} else  {iplus = i+1;}
		if (j == Ynodes-1) {jplus = j;} else  {jplus = j+1;}
	
	  int I, J;
	  
	  if(min_abs(*BloodVesselIndicatorX(i,j))>1.5||*BloodVesselIndicatorX(i,j)>0.5)
	  {
	  I=iplus;
	  J=j;
	  Ratio = *WssX(i,j)/MaxWss;
	  
	  double RandNumber = float(rand())/float(RAND_MAX); 
	// if()
	 {
	   if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=1;}
	  else if(*BloodVesselIndicatorX(I,J)<-0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
		}
	  
	  }
	 	  
	   
		
	  
	 
	  
	
	  I=iminus;
	  J=j;
	  if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
	  {*BloodVesselIndicatorX(I,J)=-1;}
	  else if(*BloodVesselIndicatorX(I,J)>0.5)
	  {*BloodVesselIndicatorX(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
	
	 
	  I=i;
	  J=jplus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=1;}
	  else if(*BloodVesselIndicatorY(I,J)<-0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));

	  
	  I=i;
	  J=jminus;
	  if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
	  {*BloodVesselIndicatorY(I,J)=-1;}
	  else if(*BloodVesselIndicatorY(I,J)>0.5)
	  {*BloodVesselIndicatorY(I,J)=0;}
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	  I=i;
	  J=j;
	  *BloodVesselIndicatorX(I,J)=0;
	  *BloodVesselIndicatorY(I,J)=0;	 	  
	  *BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
	
		
	
	

	
	
	
	

	
	
	
	 pTempVesselCell = TheVessel->FindCell(i,j);
	 
	
	
	
	
	 pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
	}
	
		 //cout<<"beginmove";
		 
	// cout<<"aftermove";
    }//this is for the loop
   pLastVesselPoint = pCurrentVesselPoint2;
   QuickPlot();// COMBINE
   cout <<"function Cleanup finished"<<endl; 

}

void VesselStructure::InitialGrowSprout(CellVesselStructure* TheVessel)
{
	cout <<"function GrowSprout running"<<endl; 
	extern bool VesselBroke;
	extern int touchpoints;
	extern Matrix SproutTips;
	extern Matrix SproutAge;
	extern Matrix VesselAge;
	//extern Matrix DirectionX;
	//extern Matrix DirectionY;
	extern Matrix BloodVesselIndicatorX;
	extern Matrix BloodVesselIndicatorY;
	extern Matrix BloodVesselIndicator;
	extern Matrix TAF;
	extern Matrix ECM;
	extern Matrix CellColor;
	extern int Xnodes, Ynodes;
	double Chemo_Response = RetrieveDoubleParameter( ParameterFile , "Chemo_Response");
	double Dif_ECM = RetrieveDoubleParameter( ParameterFile , "Dif_ECM");
	double Chemo_Sense = RetrieveDoubleParameter( ParameterFile , "Chemo_Sense");
	double String = RetrieveDoubleParameter( ParameterFile , "String");
	double Haptotactic_Reponse = RetrieveDoubleParameter( ParameterFile , "Haptotactic_Reponse");
	AngioDeltaX = 1.0 /Xnodes;// here...I don't agree Min's Note
	AngioDeltaY = 1.0 /Ynodes;
	Matrix Prob0 = zeros(Xnodes, Ynodes);
	Matrix Prob1 = zeros(Xnodes, Ynodes);
	Matrix Prob2 = zeros(Xnodes, Ynodes);
	Matrix Prob3 = zeros(Xnodes, Ynodes);
	Matrix Prob4 = zeros(Xnodes, Ynodes);
	Matrix Inf0 = zeros(Xnodes, Ynodes);
	Matrix Inf1 = zeros(Xnodes, Ynodes);
	Matrix Inf2 = zeros(Xnodes, Ynodes);
	Matrix Inf3 = zeros(Xnodes, Ynodes);
	Matrix Inf4 = zeros(Xnodes, Ynodes);
	Matrix Sup0 = zeros(Xnodes, Ynodes);
	Matrix Sup1 = zeros(Xnodes, Ynodes);
	Matrix Sup2 = zeros(Xnodes, Ynodes);
	Matrix Sup3 = zeros(Xnodes, Ynodes);
	Matrix Sup4 = zeros(Xnodes, Ynodes);
	int iminus,iplus,jminus,jplus;
	bool growstill=1;
	bool setorigin=0;
	int LoopLength = VesselListLength; 
	cout<<"Growth Begin"<<endl;
	VesselPoint* pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
	VesselPoint* pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
	VesselCell* pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
	VesselCell* pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
	VesselCell* pTempVesselCell = TheVessel->pFirstVesselCell;
	
	while(growstill || !setorigin)
	{
		
		cout<<"Once Begin"<<endl;
		growstill = 0;
		
		
		Prob0 = zeros(Xnodes, Ynodes);
		Prob1 = zeros(Xnodes, Ynodes);
		Prob2 = zeros(Xnodes, Ynodes);
		Prob3 = zeros(Xnodes, Ynodes);
		Prob4 = zeros(Xnodes, Ynodes);
		Inf0 = zeros(Xnodes, Ynodes);
		Inf1 = zeros(Xnodes, Ynodes);
		Inf2 = zeros(Xnodes, Ynodes);
		Inf3 = zeros(Xnodes, Ynodes);
		Inf4 = zeros(Xnodes, Ynodes);
		Sup0 = zeros(Xnodes, Ynodes);
		Sup1 = zeros(Xnodes, Ynodes);
		Sup2 = zeros(Xnodes, Ynodes);
		Sup3 = zeros(Xnodes, Ynodes);
		Sup4 = zeros(Xnodes, Ynodes);
		
		
		pCurrentVesselPoint1 = pFirstVesselPoint; //This is used to sweep along the previous vessel point list
		pCurrentVesselPoint2 = pLastVesselPoint; //This is used to add points to new vessel point list
		pCurrentVesselCell1 = TheVessel->pFirstVesselCell;//This is used to find out the current cell respond to the point
		pCurrentVesselCell2 = TheVessel->pLastVesselCell;//This is used to add cells to new vessel cell list
		pTempVesselCell = TheVessel->pFirstVesselCell;
		LoopLength = VesselListLength;
		VesselBroke=0;
		
		
		for(int Loop = 0; Loop < LoopLength; Loop++)
		{
			//cout<<"enter loop"<<endl;
			int i = pCurrentVesselPoint1->i;
			int j = pCurrentVesselPoint1->j;
			int i_origin = pCurrentVesselPoint1->i_origin;
			int j_origin = pCurrentVesselPoint1->j_origin;
			
			
			
			if((i==0||j==0||i==Xnodes-1||j==Ynodes-1)&&*SproutTips(i,j)==1)
			{
				*SproutTips(i,j)=0;
				//SproutNumber -=1;
			}
			
			
			if( *SproutTips(i,j) > 0)
			{
				
				
				//cout<<"enter sprouttip loop"<<endl;
				//cout<<"beginsprout";
				if (i == 0) {iminus=1;} else  {iminus = i-1;} //Again, it's strange, sometimes they set boundary periodically, sometimes like neumann, sometimes D...
				if (j == 0) {jminus=1;} else  {jminus = j-1;} //it's not in use actually...so i can difine whatever i want
				if (i == Xnodes-1) {iplus = Xnodes - 2;} else  {iplus = i+1;}
				if (j == Ynodes-1) {jplus = Ynodes - 2;} else  {jplus = j+1;}
				
				//Prob0 should be with stability 
				
				/*
				 *Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i, j) );
				 
				 *Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i, j) );
				 
				 *Prob3(i, j) = Chemo_Response*( *TAF(i, jplus) - *TAF(i, j) );	   
				 
				 *Prob4(i, j) = Chemo_Response*( *TAF(i, jminus) - *TAF(i, j) );
				 */
				*Prob0(i, j) = 0;
				*Prob1(i, j) = 0;
				*Prob2(i, j) = 0;
				*Prob3(i, j) = 0;
				*Prob4(i, j) = 0;
				if( *TAF(iplus, j) - *TAF(i, j) > 0.0)
				{
					*Prob1(i, j) =  Chemo_Response*( *TAF(iplus, j) - *TAF(i_origin, j_origin) ) - String*pow((iplus-i_origin)*(iplus-i_origin)+(j-j_origin)*(j-j_origin), 0.5);
					// cout<< "*Prob1(i, j)"<<*Prob1(i, j)<<endl;
				}
				if(*TAF(iminus, j) - *TAF(i, j) > 0.0)
				{
					*Prob2(i, j) =  Chemo_Response*( *TAF(iminus, j) - *TAF(i_origin, j_origin) ) - String*pow((iminus-i_origin)*(iminus-i_origin)+(j-j_origin)*(j-j_origin), 0.5);
					//	  cout<< "*Prob2(i, j)"<<*Prob2(i, j)<<endl;
				}
				if(*TAF(i, jplus) - *TAF(i, j) > 0.0)
				{
					//	cout<< "*Prob3(i, j)"<<*Prob3(i, j)<<endl;
					*Prob3(i, j) = Chemo_Response*( *TAF(i,jplus) - *TAF(i_origin, j_origin) )  - String*pow((i-i_origin)*(i-i_origin)+(jplus-j_origin)*(jplus-j_origin), 0.5);	   
				}
				if(*TAF(i, jminus) - *TAF(i, j) > 0.0)
				{
					
					//	cout<< "*Prob4(i, j)"<<*Prob4(i, j)<<endl;
					*Prob4(i, j) = Chemo_Response*(  *TAF(i,jminus) - *TAF(i_origin, j_origin) ) - String*pow((i-i_origin)*(i-i_origin)+(jminus-j_origin)*(jminus-j_origin), 0.5);
				}
				
				
				
				
				
				if( *Prob0(i,j) <= 0 )
				{
					*Prob0(i,j) = 0;
				}
				if( *Prob1(i,j) <= 0 )
				{
					*Prob1(i,j) = 0;
					
				}
				if( *Prob2(i,j) <= 0 )
				{
					*Prob2(i,j) = 0;
					
				}
				if( *Prob3(i,j) <= 0 )
				{
					*Prob3(i,j) = 0;
					
				}
				
				if( *Prob4(i,j) <= 0 )
				{
					*Prob4(i,j) = 0;
					
				}
				
				
				
				
				
				//the sprout shouldn't grow reverse  
				if (*BloodVesselIndicatorX(i,j) < 0 )
				{
					*Prob2(i,j) = 0;
				}
				else if (*BloodVesselIndicatorX(i,j) > 0)
				{
					*Prob1(i,j) = 0;
				}
				else if (*BloodVesselIndicatorY(i,j) < 0 )
				{
					*Prob4(i,j) = 0;
				}
				else if (*BloodVesselIndicatorY(i,j) > 0 )
				{
					*Prob3(i,j) = 0;
				}
				//other exceptions 
				if (i == Xnodes-1) 
				{ *Prob1(i, j) = 0.0; }
				else if (i == 0)   
				{ *Prob2(i, j) = 0.0; }
				if (j == ( Ynodes-1))
				{ *Prob3(i, j) = 0.0; }
				if (j == 0)
				{ *Prob4(i, j) = 0.0; }
				
				
				// cout<<*Prob0(i,j)<<*Prob1(i,j) <<*Prob2(i,j)<<*Prob3(i,j) <<*Prob4(i,j)<<endl;
				//rescale the probabilities
				double ProbTotal = *Prob0(i,j) + *Prob1(i,j) + *Prob2(i,j) + *Prob3(i,j) + *Prob4(i,j) ;
				if ( ProbTotal> 0 )
				{
					growstill = 1;
					
					*Prob0(i,j) /= ProbTotal;
					*Prob1(i,j) /= ProbTotal;
					*Prob2(i,j) /= ProbTotal;
					*Prob3(i,j) /= ProbTotal;
					*Prob4(i,j) /= ProbTotal;
					
					cout<<*Prob0(i,j)<<"  "<<*Prob1(i,j)<<"  " <<*Prob2(i,j)<<"  "<<*Prob3(i,j)<<"  " <<*Prob4(i,j)<<endl;
					
					//Get Random Intervals
					*Inf0(i,j) = 0;
					*Sup0(i,j) = *Inf0(i,j) + *Prob0(i,j);
					//*Sup0(i,j) = *Inf0(i,j) + *Prob1(i,j);
					*Inf1(i,j) = *Sup0(i,j);
					*Sup1(i,j) = *Inf1(i,j) + *Prob1(i,j);
					//*Sup1(i,j) = *Inf1(i,j) + *Prob0(i,j);
					*Inf2(i,j) = *Sup1(i,j);
					*Sup2(i,j) = *Inf2(i,j) + *Prob2(i,j);
					*Inf3(i,j) = *Sup2(i,j);
					*Sup3(i,j) = *Inf3(i,j) + *Prob3(i,j);
					//*Sup3(i,j) = *Inf3(i,j) + *Prob4(i,j);
					*Inf4(i,j) = *Sup3(i,j);   //not neccesary, delete them to speed up the program
					*Sup4(i,j) = *Inf4(i,j) + *Prob4(i,j);
					//*Sup4(i,j) = *Inf4(i,j) + *Prob3(i,j);
					
					//Done with those probabilities, begin to Move and Connect in one step.
					double RandNumber;
					int I, J;
					RandNumber = float( rand() ) / float( RAND_MAX);
					
					if(RandNumber >=*Inf0(i,j) && RandNumber < *Sup0(i,j))
					{
						
					}
					
					else if(RandNumber >=*Inf1(i,j) && RandNumber < *Sup1(i,j))
					{
						I = i + 1;
						J = j;
						
						
						if( (min_abs(*BloodVesselIndicator(I, J))) > 0 )//*SproutTips(I, J) > 0 || *DirectionY(I, J) > 0 || *DirectionY(I, J + 1) > 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; //because we will age a delta t later in the else
								SproutNumber -= 1;//comment this we have anasmosis
								
							}
							
							
							
							//*DirectionX(I, J) = 1;
							//*SproutTips(I, J) = 1;//Set this to 0 we have anasmosis,1 is they can overlap
							//SproutNumber -=1;//Add this we have anasmosis
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;
							*SproutTips(i, j) = 0;	
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
							
							
							
							
						}
						else
						{
							
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob1 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionX(I, J) = 1;
							
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
							
							
							
						}
						
						
						
						if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
						{
							*BloodVesselIndicatorX(i,j)=min_abs(*BloodVesselIndicatorX(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorX(i,j)<-0.5)
							{*BloodVesselIndicatorX(i,j)=2;}
							else
							{*BloodVesselIndicatorX(i,j)=1;}
						}
						if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
						{
							*BloodVesselIndicatorX(I,J)=min_abs(*BloodVesselIndicatorX(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorX(I,J)>0.5)
							{*BloodVesselIndicatorX(I,J)=2;}
							else
							{*BloodVesselIndicatorX(I,J)=-1;}
						}
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
						
						
					}
					
					else if(RandNumber >= *Inf2(i,j) && RandNumber < *Sup2(i,j))
					{
						I = i - 1;
						J = j;
						
						
						if(  (min_abs(*BloodVesselIndicator(I, J))) > 0 )
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;	
								
								
							}
							
							
							// *DirectionX(I, J) = -1;
							// *SproutTips(I, J) = 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	
							//SproutNumber -= 1;
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob2 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionX(I, J) = -1;
							
							
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
							
						}
						
						
						if(min_abs(*BloodVesselIndicatorX(i,j))>1.5)
						{
							*BloodVesselIndicatorX(i,j)=-min_abs(*BloodVesselIndicatorX(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorX(i,j)>0.5)
							{*BloodVesselIndicatorX(i,j)=2;}
							else
							{*BloodVesselIndicatorX(i,j)=-1;}
						}
						if(min_abs(*BloodVesselIndicatorX(I,J))>1.5)
						{
							*BloodVesselIndicatorX(I,J)=-min_abs(*BloodVesselIndicatorX(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorX(I,J)<-0.5)
							{*BloodVesselIndicatorX(I,J)=2;}
							else
							{*BloodVesselIndicatorX(I,J)= 1;}
						}
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					else if(RandNumber >=*Inf3(i,j) && RandNumber < *Sup3(i,j))
					{
						I = i;
						J = j + 1;
						
						if( (*BloodVesselIndicator(I, J))> 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;
								
								
							}
							
							
							
							// SproutNumber -= 1;
							// *DirectionY(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	 
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob3 make Vessel Length"<<VesselListLength<<endl;
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionY(I, J) = 1;
							
							
							//cout<<"here"<<endl;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							//cout<<pCurrentVesselCell1->cIndex<<endl;
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//cout<<pCurrentVesselCell2->cIndex<<endl;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
						}
						
						if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
						{
							*BloodVesselIndicatorY(i,j)=min_abs(*BloodVesselIndicatorY(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorY(i,j)<-0.5)
							{*BloodVesselIndicatorY(i,j)=2;}
							else
							{*BloodVesselIndicatorY(i,j)=1;}
						}
						if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
						{
							*BloodVesselIndicatorY(I,J)=min_abs(*BloodVesselIndicatorY(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorY(I,J)>0.5)
							{*BloodVesselIndicatorY(I,J)=2;}
							else
							{*BloodVesselIndicatorY(I,J)=-1;}
						}
						
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					else if(RandNumber >= *Inf4(i,j) && RandNumber < *Sup4(i,j))
					{
						I = i;
						J = j - 1;
						
						
						if( (*BloodVesselIndicator(I, J)) > 0)
						{
							
							if( *SproutTips(I, J) > 0) 
							{
								*VesselAge(I, J) -= AngioDeltaT; 
								SproutNumber -= 1;
							}
							
							
							// *DirectionY(I, J) = -1;
							// *SproutTips(I, J) = 1;
							//SproutNumber -= 1;
							*SproutAge(i, j) = 0;
							*SproutAge(I, J) = 0;	
							*SproutTips(i, j) = 0;
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							pTempVesselCell = TheVessel->FindCell(I,J);
							Connect(pCurrentVesselCell1,pTempVesselCell);
							
							
						}
						else
						{		
							
							VesselPoint* pNewVesselPoint = new VesselPoint;
							pNewVesselPoint->pPreviousVesselPoint = pCurrentVesselPoint2;
							
							pCurrentVesselPoint2->pNextVesselPoint = pNewVesselPoint;	
							pCurrentVesselPoint2 = pCurrentVesselPoint2->pNextVesselPoint;
							pCurrentVesselPoint2->i = I;
							pCurrentVesselPoint2->j = J;
							pCurrentVesselPoint2->i_origin = i_origin;
							pCurrentVesselPoint2->j_origin = j_origin;
							pCurrentVesselPoint2->pNextVesselPoint = NULL;
							VesselListLength++;
							//cout<<"Prob4 make Vessel Length"<<VesselListLength<<endl;
							
							
							*SproutTips(i, j) = 0;
							*SproutTips(I, J) = 1;
							//*SproutTips(I, J) = 1;
							*SproutAge(I, J) = *SproutAge(i, j) + AngioDeltaT;
							*SproutAge(i, j) = 0;
							//*DirectionY(I, J) = -1;
							
							pCurrentVesselCell1 = TheVessel->FindCell(i,j);
							
							double Prob = min_prob(*TAF(i,j), pCurrentVesselCell1->cCover, pCurrentVesselCell1->coverlimit);
							double sezi;
							sezi= float( rand() ) / float( RAND_MAX);
							if(sezi<Prob)
							{
								
								TheVessel->CellListLength++;
								VesselCell* pNewVesselCell = new VesselCell;
								pNewVesselCell->pPreviousVesselCell = pCurrentVesselCell2;
								pCurrentVesselCell2->pNextVesselCell = pNewVesselCell;	
								pCurrentVesselCell2 = pCurrentVesselCell2->pNextVesselCell;
								pCurrentVesselCell2->cIndex = TheVessel->CellListLength;
								pCurrentVesselCell2->cCover = 1;
								pCurrentVesselCell2->i[0] = I;
								pCurrentVesselCell2->j[0] = J;
								pCurrentVesselCell2->pNextVesselCell = NULL;
								//pCurrentVesselCell2->cConnection = 1;
								//pCurrentVesselCell2->cConnectIndex[0] = pCurrentVesselCell1->cIndex;
								Connect(pCurrentVesselCell2,pCurrentVesselCell1);
								*CellColor(I,J) = 20;
								
								
							}//mitosis and grow
							else
							{
								
								pCurrentVesselCell1->cCover = pCurrentVesselCell1->cCover+1;
								pCurrentVesselCell1->i[pCurrentVesselCell1->cCover-1] = I;
								pCurrentVesselCell1->j[pCurrentVesselCell1->cCover-1] = J;
								
								*CellColor(I,J) = 100;
								
							}//justgrow
							
							
						}
						
						if(min_abs(*BloodVesselIndicatorY(i,j))>1.5)
						{
							*BloodVesselIndicatorY(i,j)=-min_abs(*BloodVesselIndicatorY(i,j));
						}
						else
						{
							if(*BloodVesselIndicatorY(i,j)>0.5)
							{*BloodVesselIndicatorY(i,j)=2;}
							else
							{*BloodVesselIndicatorY(i,j)=-1;}
						}
						if(min_abs(*BloodVesselIndicatorY(I,J))>1.5)
						{
							*BloodVesselIndicatorY(I,J)=-min_abs(*BloodVesselIndicatorY(I,J));
						}
						else
						{
							if(*BloodVesselIndicatorY(I,J)<-0.5)
							{*BloodVesselIndicatorY(I,J)=2;}
							else
							{*BloodVesselIndicatorY(I,J)=1;}
						}
						
						
						*BloodVesselIndicator(i, j) = min_abs(*BloodVesselIndicatorX(i,j)) + min_abs(*BloodVesselIndicatorY(i,j));
						*BloodVesselIndicator(I, J) = min_abs(*BloodVesselIndicatorX(I,J)) + min_abs(*BloodVesselIndicatorY(I,J));
						
					}
					
					//BOUNDARY SPROUTS We can ask it to be periodical?
				}//probtotal>0
				else
				{
					//cout<<"Show if i come here"<<endl;
					//getchar();
					*SproutTips(i,j) = 0;
				}//probtotal=0
				
			}//this is for the if sprout
			
			
			pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
			//cout<<"aftermove";
		}//this is for the loop
		pLastVesselPoint = pCurrentVesselPoint2;
		TheVessel->pLastVesselCell = pCurrentVesselCell2;
		//QuickPlot();// COMBINE
		cout <<"GrowSprout once"<<endl; 
		if(!growstill)
		{
			
			pCurrentVesselPoint1 = pFirstVesselPoint;
			
			
			
			while(pCurrentVesselPoint1)
			{
				pCurrentVesselPoint1->i_origin = pCurrentVesselPoint1->i;
				pCurrentVesselPoint1->j_origin = pCurrentVesselPoint1->j;
				
				
				
				pCurrentVesselPoint1 = pCurrentVesselPoint1->pNextVesselPoint;
			}
			
			
			
			
			setorigin = 1;
			
			
			
			
			cout <<"origin reset"<<endl; 
			
		}//setorigin 
		
		
		
	}//still loop
	cout <<"function GrowSprout finish"<<endl;
}

